2016-03-17 95 views
0

我想在請求正文中發送JSON。服務器將預期請求標頭指示正文爲JSON。在請求正文中發送JSON

這裏是我的代碼:

JSONObject data = new JSONObject(); 
     try { 
      data.put("id", username); 
      data.put("latitude", latitude); 

RequestQueue queue = Volley.newRequestQueue(this); 

     JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,data, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         System.out.println(response); 
         //hideProgressDialog(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         VolleyLog.d("TAG", "Error: " + error.getMessage()); 
         // hideProgressDialog(); 
        } 
       }); 
     queue.add(jsObjRequest); 
    } 

當我跑我的 「URL」 越來越BasicNetwork.performRequest: Unexpected response code 400程序。 請幫我解決這個

回答

1

試試這個代碼

RequestParams data = new RequestParams(); 
     data.put("id", username); 
     data.put("latitude", latitude); 

RestClient.post(getHost() + "url", data, 
     new JsonHttpResponseHandler() { 

    @Override 
    public void onSuccess(int statusCode, Header[] headers, 
      JSONObject response) { 

     try { 

      //process JSONObject obj 
      Log.w("myapp","success status code..." + statusCode); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onFailure(int statusCode, Header[] headers, 
      Throwable throwable, JSONObject errorResponse) { 
     Log.w("myapp", "failure status code..." + statusCode); 


     try { 
      //process JSONObject obj 
      Log.w("myapp", "error ..." + errorResponse.getString("message").toString()); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}); 
0

你缺少關於類型的標題內容的信息。覆蓋請求對象中的以下方法。

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,data, 
       new Response.Listener<JSONObject>() { 

//other methods 

    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     HashMap<String, String> headers = new HashMap<String, String>(); 
     headers.put("Content-Type", "application/json; charset=utf-8"); 
     return headers; 
    } 
}); 
0

400意味着客戶端錯誤,這意味着您要發送的json或頭是錯誤的。只要你JsonObjectRequest方法下添加下面的代碼塊頂部的

@Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headerMap = new HashMap<String,String>(); 
      headerMap.put("Content-Type", "application/json"); 
      headerMap.put("Accept", "application/json"); 
      return headerMap; 
     } 

像下面

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,data, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        System.out.println(response); 
        //hideProgressDialog(); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d("TAG", "Error: " + error.getMessage()); 
        // hideProgressDialog(); 
       } 
      }){ 
       @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headerMap = new HashMap<String,String>(); 
      headerMap.put("Content-Type", "application/json"); 
      headerMap.put("Accept", "application/json"); 
      return headerMap; 
     } 
      }; 
    queue.add(jsObjRequest); 
相關問題