2015-04-24 24 views
1

我試圖使用擴展JsonRequest類的自定義類發送JSONArrayRequest使用POST和參數。使用自定義凌空POST不返回任何

public class MethodJsonArrayRequest extends JsonRequest<JSONArray> { 
    public MethodJsonArrayRequest(int method, String url, JSONObject params, com.android.volley.Response.Listener<org.json.JSONArray> listener, ErrorListener errorListener) { 
     super(method, url, params.toString(), listener, errorListener); 
     Log.d("method", Integer.toString(method)); 
     Log.d("jsonRequest", params.toString()); 
    } 

    @Override 
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { 
     try { 
      String jsonString = new String(response.data, 
        HttpHeaderParser.parseCharset(response.headers)); 
      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)); 
     } 
    } 
} 

我的日誌返回此:

D/method﹕ 1 
D/jsonRequest﹕ {"idShow":"219"} 

我傳遞這個信息到我的自定義類與此SNIPPIT:

myResponse的
... 
     JSONObject params = new JSONObject(); 
     try { 
      params.put("idShow", idShow); 
     } 
     catch (JSONException e) {Log.d("JSON e", e.getMessage()); } 

     MethodJsonArrayRequest episodeRequest = new MethodJsonArrayRequest(Request.Method.POST, episodeURL, params, new Response.Listener<JSONArray>() { 
     @Override 
     public void onResponse(JSONArray myResponse) { 
      try { 
       Log.d("myResponse", myResponse.toString()); 
... 

登錄:

D/myResponse﹕ [] 

但無論出於何種原因,它都不會返回任何事情,我覺得我可能不會爲paramas傳遞正確的東西,但我不確定,任何幫助都非常感謝!讓我知道,如果有什麼我沒有包括在這裏可能會有所幫助。

+0

爲了讓事情更有趣一些,我也嘗試過使用GET(修改我的PHP代碼以期望相同)並獲得相同的結果。爲了測試它不是我的PHP,我創建了一個快速簡單的HTML頁面,它傳遞GET或POST並返回我正在尋找的JSON數組。 – Wingman1487

回答

0

user98239820答案here是extreamly有用。他沒有延長JsonRequest<JSONArray>班,而是延長了Request班。我也必須將他的new JSONObject更改爲new JSONArray以符合我的需求,但指出該類完美。