【问题标题】:Android Not Resolving Volley Method.GETAndroid 无法解析 Volley Method.GET
【发布时间】:2016-01-04 08:05:24
【问题描述】:

我是 Android 新手,只是尝试按照 Android 网站上的说明使用 Volley,但没有找到正确的方法。

这行似乎引起了麻烦:

StringRequest stringRequest = new StringRequest(VoiceInteractor.Request.Method.GET, url,

错误是:

Error:(32, 92) error: cannot find symbol variable Method

MainActivity.java:

package com.jorc.volley;

import android.app.VoiceInteractor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

          final TextView mTextView = (TextView) findViewById(R.id.text);
            // Instantiate the RequestQueue.
                    RequestQueue queue = Volley.newRequestQueue(this);
                    String url ="http://www.google.com";

            // Request a string response from the provided URL.
                    StringRequest stringRequest = new StringRequest(VoiceInteractor.Request.Method.GET, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    // Display the first 500 characters of the response string.
                                    mTextView.setText("Response is: "+ response.substring(0,500));
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            mTextView.setText("That didn't work!");
                        }
                    });
            // Add the request to the RequestQueue.
                    queue.add(stringRequest);
    }
}

【问题讨论】:

  • 你的服务方法类型是get还是post??
  • 这似乎解决了问题,谢谢 ρяσѕρєя K

标签: android get android-volley


【解决方案1】:

使用 Volley 库中的 Request.Method.GETStringRequest,而不是用于 voice interaction requestsVoiceInteractor.Request

StringRequest stringRequest = new StringRequest(Request.Method.GET,... url,

【讨论】:

    【解决方案2】:

    这是 android 的官方文档所说的关于 volley requests Sending a Simple Request

    简单的排球请求

     final TextView mTextView = (TextView) findViewById(R.id.text);
        ...
    
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://www.google.com";
    
        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // Display the first 500 characters of the response string.
                mTextView.setText("Response is: "+ response.substring(0,500));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mTextView.setText("That didn't work!");
            }
        });
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    

    如果您要发送 JSON 请求,那么您可以这样做,这是我自己使用 Volley 库发送 JSON 请求的方式

    使用 Volley 的 JSON 请求

    public void myJSONRequest(){
      JSONObjectRequest request = new JSONObjectRequest(
       Request.Method.GET,
       url, 
       null,
       new Response.Listener<JSONObject>(){
         public void onResponse(JSONResponse response){
          // Do you parsing 
         }
       },
       new Response.ErrorListener(){
         public void onErrorResponse(VolleyError volleyError){
          // handle your errors
         }
       }
      );
      if(request != null){
        requestQueue.add(request);
      }
      else{
         Log.d("ERROR", " Volley Request Failed");
      }
    }
    

    【讨论】:

    • 你不是在复制上面的答案吗?
    • @Amir 不,这只是兴奋,因为我也是 android 的新手,我最近学习了这个东西(Volley),在那之前我使用的是 HttpUrlConnection 类,它太复杂了,而且 volley 有点容易,这就是为什么我只回答这个问题,我用这种风格(Request.Method.GET)编写我的代码,我教你没有正确编写语句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多