2016-03-17 71 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); 
相关问题