2017-02-14 150 views
0

我正在制作一个与Facebook API集成并使用REST API的Android应用程序,因此我使用了Volley。但是,我试图为JSON数组发出GET请求,并且必须包含Facebook授权令牌才能访问服务器。大部分时间我已经看到了这个问题都比较陈旧,而且好像现在凌空提供支持的请求参数传递(从凌空github上页):使用Volley GET请求参数

/** 
* Creates a new request. 
* @param method the HTTP method to use 
* @param url URL to fetch the JSON from 
* @param jsonRequest A {@link JSONArray} to post with the request. Null is allowed and 
* indicates no parameters will be posted along with request. 
* @param listener Listener to receive the JSON response 
* @param errorListener Error listener, or null to ignore errors. 
*/ 
public JsonArrayRequest(int method, String url, JSONArray jsonRequest, 
         Listener<JSONArray> listener, ErrorListener errorListener) { 
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, 
      errorListener); 
} 

但是当我做,并出具JsonArrayRequest ,我得到一个com.android.volley.AuthFailureError。这是我的代码,有谁知道我做错了什么?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    System.out.println("Yo im here"); 
    JSONObject requestParams = new JSONObject(); 
    JSONArray paramArray = new JSONArray(); 
    try { 
     requestParams.put("Authorization", "JWT (Facebook auth token)"); 
     paramArray.put(requestParams); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    System.out.println(paramArray.toString()); 

    RequestQueue queue = Volley.newRequestQueue(this); 

    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, 
      GET_MAP_MICS, 
      paramArray, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray micArray) { 
        try { 
         for (int i = 0; i < micArray.length(); i++) { 
          JSONObject jsonobject = micArray.getJSONObject(i); 
          int micId = jsonobject.getInt("micId"); 
          String status = jsonobject.getString("status"); 
          System.out.println("Good so far!"); 
          double venLat = jsonobject.getDouble("venueLat"); 
          double venLong = jsonobject.getDouble("venueLat"); 
          System.out.println("got here, check it: " + venLat); 
         } 
        }catch (JSONException e) { 
          e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError e) { 
      System.out.println(e); 
     } 
    }); 

回答

0

GET方法不带参数仅供POST

在你的情况下,你的传球头球为体 GET方法

你有通头

JsonObjectRequest request = new JsonObjectRequest(url, (JSONObject) null, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject jsonObject) { 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError volleyError) { 

      } 
     }) { 
      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String, String> map = new HashMap<>(); 
       /*Add your headers here*/ 
       return super.getHeaders(); 
      } 
     }; 

最终可能在两个相同的类名字将在你的项目中存在,例如你使用的任何lib也使用volley lib这种情况​​只发生一次

+0

感谢你们,我误认了参数和标题之间的区别。但是,您发布的方式不是覆盖getHeaders方法的有效方法,那么覆盖该方法的正确方法是什么? –

+0

只有方法通过标题是这样的'volley'中的任何'req'通过使用'Map '如果有其他方法让我知道如果你发现它 –

+0

否我只是说我得到一个语法尝试添加getHeaders的覆盖时发生错误 –