2016-12-21 40 views
0

我wan't從JsonArrayRequest後發送到服務器,我發現了一些答案,我想它..機器人 - 凌空JsonArrayRequest加入StringRequest後

,但我得到了這樣的like this

錯誤,這是我的代碼

HashMap<String, String> params = new HashMap<String, String>(); 

public void JSON_DATA_WEB_CALL() { 
     jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL, new JSONObject(params), 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 

         JSON_PARSE_DATA_AFTER_WEBCALL(response); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

        } 
       }){ 
      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       HashMap<String, String> headers = new HashMap<String, String>(); 
       headers.put("CUSTOM_HEADER", "Yahoo"); 
       headers.put("ANOTHER_CUSTOM_HEADER", "Google"); 
       return headers; 
      } 
     }; 
     requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(jsonArrayRequest); 
    } 

如何解決這個問題?

+0

閱讀這篇文章:http://stackoverflow.com/questions/24358402/android-volley-post-request-workaround-for -jsonarrayrequest?rq = 1 – Stanojkovic

+0

對不起,但如何在我的情況下執行? –

回答

0

使用此自定義類。你的情況

public class CustomJsonArrayRequest extends JsonRequest<JSONArray> { 
/** 
* Creates a new request. 
* @param method the HTTP method to use 
* @param url URL to fetch the JSON from 
* @param jsonRequest A {@link JSONObject} 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 CustomJsonArrayRequest(int method, String url, JSONObject jsonRequest, 
           Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) { 
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, 
      errorListener); 
} 

@Override 
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { 
    try { 
     String jsonString = new String(response.data, 
       HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); 
     return Response.success(new JSONArray(jsonString), 
       HttpHeaderParser.parseCacheHeaders(response)); 
    } catch (UnsupportedEncodingException e) { 
     return Response.error(new ParseError(e)); 
    } catch (JSONException je) { 
     return Response.error(new ParseError(je)); 
    } 
} 
} 

實施應該是這樣的:

HashMap<String, String> params = new HashMap<String, String>(); 

public void JSON_DATA_WEB_CALL() { 
    CustomJsonArrayRequest request = new CustomJsonArrayRequest (Request.Method.POST, GET_JSON_DATA_HTTP_URL, new JSONObject(params), 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 

        JSON_PARSE_DATA_AFTER_WEBCALL(response); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }){ 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("CUSTOM_HEADER", "Yahoo"); 
      headers.put("ANOTHER_CUSTOM_HEADER", "Google"); 
      return headers; 
     } 
    }; 
    requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(request); 
} 
+0

我有錯誤'PROTOCOL_CHARSET'在'com.andoir.volley.toolbox.JsonRequest'有私人訪問..如何解決? –

+0

你可以發佈你迄今爲止做了什麼嗎?另外,錯誤的堆棧跟蹤? – jlively

+0

這個CustomJsonArrayRequest http://pastebin.com/KSFZZB1e,而這個活動http://pastebin.com/s8rx3YDQ –