2015-11-22 57 views
0

CustomRequest.java獲得500錯誤凌空PUT請求的Android

public class CustomRequest extends Request<JSONObject> { 

    private Listener<JSONObject> listener; 
    private Map<String, String> params; 
    private Map<String, String> headers; 

    public CustomRequest(String url, 
         Map<String, String> params, 
         Map<String, String> headers, 
         Listener<JSONObject> reponseListener, 
         ErrorListener errorListener) { 
     super(Method.GET, url, errorListener); 
     this.listener = reponseListener; 
     this.params = params; 
     this.headers = headers; 
    } 

    public CustomRequest(int method, 
         String url, 
         Map<String, String> params, 
         Map<String, String> headers, 
         Listener<JSONObject> reponseListener, 
         ErrorListener errorListener) { 
     super(method, url, errorListener); 
     this.listener = reponseListener; 
     this.params = params; 
     this.headers = headers; 
    } 

    protected Map<String, String> getParams() 
      throws com.android.volley.AuthFailureError { 
     return params; 
    }; 

    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     return headers != null ? headers : super.getHeaders(); 
    } 

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

    @Override 
    protected void deliverResponse(JSONObject response) { 
     // TODO Auto-generated method stub 
     listener.onResponse(response); 
    } 

我在我的類使用像這樣的請求:

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

     headers.put("Content-Type", "application/json"); 

     Iterator it = params.entrySet().iterator(); 
     while (it.hasNext()) { 
      Map.Entry pairs = (Map.Entry) it.next(); 
      Log.d(context.getClass().getCanonicalName(), "params - " + pairs.getKey() + ", " + pairs.getValue()); 
     } 

     String url = AppConstants.SERVER_URL + mApi + "/" + mType+ "/" +mWord.getID(); 

     Log.d("FinalUrl:->", url); 

     RequestQueue requestQueue = Volley.newRequestQueue(ActDrawAreaTwo.this); 
     CustomRequest jsObjRequest = new CustomRequest(Request.Method.PUT, 
                 url, 
                 params, 
                 headers, 
                 this.createRequestSuccessListener(), 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         progress.dismiss(); 
        } 
       }); 
     RequestQueueHelper.addToRequestQueue(jsObjRequest, ""); 

我收到錯誤

11-22 10:28:01.605 17921-18491/com.app.admin E/Volley: [3516] BasicNetwork.performRequest: Unexpected response code 500 

它是一個PUT請求,其在郵遞員

enter image description here

+0

最可能的請求未被正確發送。你確定這些參數在那裏嗎? –

+0

看起來你沒有設置任何'params'值。 – rmtheis

回答

2

工作根據您的郵差截圖,我建議你使用下面的示例代碼:

 String url = "http://..."; 
     JSONObject jsonBody; 
     try { 
      jsonBody = new JSONObject(); 
      jsonBody.put("ID", 1); 
      jsonBody.put("Name", "Word1"); 
      jsonBody.put("ArabicName", "Arabic Word1"); 
      // other key-value pairs... 

      JsonObjectRequest request = new JsonObjectRequest(Request.Method.PUT, url, jsonBody, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        // do something 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        // do something 
       } 
      }); 

      queue.add(request); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

希望這有助於!