2015-08-28 72 views
3

我試圖做一個請求MVC服務器與web-api使用排在android中,我想發送函數的參數作爲JSON(請求正文),所以我使用JSONRequest進行調用,但問題是JSONRequest強制您使用Response.Listner,並且我調用的函數返回布爾變量,所以每次撥打電話時都會收到投射錯誤接收布爾變量與json請求凌空

我試圖做一個自定義調用,接受布爾值,但現在我得到錯誤400,我不知道爲什麼,有沒有辦法讓jsonRequest接收布爾?如果不是,解決方案是什麼?

我第一次使用的功能:

public void getJSON(String controllerName,String actionName, JSONObject requestBody,Response.Listener<JSONObject> success,Response.ErrorListener error){ 
    String url = makeURL(controllerName,actionName); 
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,url,requestBody,success,error); 
    request.setTag(APP_TAG); 
    requestQueue.add(request); 
} 

,這裏是我的第二次審判定製請求(收到錯誤400):

public void getBooleanRequest(String controllerName,String actionName, final HashMap<String,String> body, final HashMap<String,String> header, final Response.Listener<Boolean> success,Response.ErrorListener error){ 
    String url = makeURL(controllerName,actionName); 
    Request<Boolean> request = new Request<Boolean>(Request.Method.POST,url,error) { 
     @Override 
     protected Response<Boolean> parseNetworkResponse(NetworkResponse response) { 
      return Response.success(true, HttpHeaderParser.parseCacheHeaders(response)); 
     } 

     @Override 
     protected void deliverResponse(Boolean response) { 
      success.onResponse(response); 
     } 

     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      return body; 
     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      return header; 
     } 
    }; 
    request.setTag(APP_TAG); 
    requestQueue.add(request); 
} 
+0

你可以創建一個自定義的請求,然後parseNetworkResponse獲得所需的數據(布爾值) – BNK

+0

你的意思是覆蓋jsonrequest響應? –

回答

5

你可以試試我下面的示例代碼:

private class BooleanRequest extends Request<Boolean> { 
    private final Response.Listener<Boolean> mListener; 
    private final Response.ErrorListener mErrorListener; 
    private final String mRequestBody; 

    private final String PROTOCOL_CHARSET = "utf-8"; 
    private final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET); 

    public BooleanRequest(int method, String url, String requestBody, Response.Listener<Boolean> listener, Response.ErrorListener errorListener) { 
     super(method, url, errorListener); 
     this.mListener = listener; 
     this.mErrorListener = errorListener; 
     this.mRequestBody = requestBody; 
    } 

    @Override 
    protected Response<Boolean> parseNetworkResponse(NetworkResponse response) { 
     Boolean parsed; 
     try { 
      parsed = Boolean.valueOf(new String(response.data, HttpHeaderParser.parseCharset(response.headers))); 
     } catch (UnsupportedEncodingException e) { 
      parsed = Boolean.valueOf(new String(response.data)); 
     } 
     return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); 
    } 

    @Override 
    protected VolleyError parseNetworkError(VolleyError volleyError) { 
     return super.parseNetworkError(volleyError); 
    } 

    @Override 
    protected void deliverResponse(Boolean response) { 
     mListener.onResponse(response); 
    } 

    @Override 
    public void deliverError(VolleyError error) { 
     mErrorListener.onErrorResponse(error); 
    } 

    @Override 
    public String getBodyContentType() { 
     return PROTOCOL_CONTENT_TYPE; 
    } 

    @Override 
    public byte[] getBody() throws AuthFailureError { 
     try { 
      return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET); 
     } catch (UnsupportedEncodingException uee) { 
      VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", 
        mRequestBody, PROTOCOL_CHARSET); 
      return null; 
     } 
    } 
} 

然後在您的活動中:

try { 
     JSONObject jsonBody; 
     jsonBody = new JSONObject(); 
     jsonBody.put("Title", "Android Demo"); 
     jsonBody.put("Author", "BNK"); 
     jsonBody.put("Date", "2015/08/28"); 
     String requestBody = jsonBody.toString(); 
     BooleanRequest booleanRequest = new BooleanRequest(0, url, requestBody, new Response.Listener<Boolean>() { 
      @Override 
      public void onResponse(Boolean response) { 
       Toast.makeText(mContext, String.valueOf(response), Toast.LENGTH_SHORT).show(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(mContext, error.toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     // Add the request to the RequestQueue. 
     queue.add(booleanRequest); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

感謝您的回答,但這種方法忽略了我需要發送我的請求正文作爲JSON的事實 –

+0

我會試試這個,謝謝:) –