2017-01-30 70 views
2

我有一个是通过使用凌空给200响应代码邮差但不调用API时POST方法API甚至Retrofit2获得响应代码200邮递员,但不是在Android网络图书馆

这里是邮差截图:

enter image description here

这里我就是这样做的凌空库代码:

final String url = "http://xxxxxxxx.com/api/mobile/user/post/"; 

    StringRequest stringReq = new StringRequest(Request.Method.POST, url, 
      new com.android.volley.Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Log.e("onResponse ===", response + " "); 
       } 
      }, 
      new com.android.volley.Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("onErrorResponse ===", error + " "); 
       } 
      }) { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> params = new HashMap<>(); 
      params.put("Authorization", "xxxxxxxxxxxxx"); 
      return params; 
     } 

     @Override 
     public Map<String, String> getParams() { 
      HashMap<String, String> params = new HashMap<>(); 
      params.put("post_body", "test"); 
      return params; 
     } 
    }; 

    // Adding request to request queue 
    mApp.addToRequestQueue(stringReq); 
    stringReq.setRetryPolicy(new DefaultRetryPolicy(
      REQUEST_TIME, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

错误logcat的:

BasicNetwork.performRequest: Unexpected response code 500 for http://xxxxxxxxxxx.com/api/mobile/user/post/ 
+0

能否请您分享您的清单? 您是否添加了以下权限? “android.permission.INTERNET” “android.permission.ACCESS_NETWORK_STATE” –

+0

你这个男人毫不怀疑。 –

+0

500是服务器错误。我对这些情况的体验通常与头文件有关。邮差默认添加了一些翻新和排除的标题。有时候服务器无法应对这种缺少头部并投掷500。我建议你检查一下。这可能是另一种方式。 – Fred

回答

6

问题是,您的端点假设multipart/form-data请求(因为邮递员中的橙色针脚设置为form-data单选按钮),而Volley的默认内容类型为StringRequestapplication/x-www-form-urlencoded,这就是为什么您会得到500错误。

因此,如果您只需要在多部分请求中发送POST参数,请检查this answer。但是,如果你要发送的文件,那么请another answer

0

你为什么不使用JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, JSONObject, response, error);的POST请求很容易使用试一次

+0

这不起作用 –

+0

这不是图书馆的问题,它的标题,你的传球,你可以检查与ION网络库为Android或检查邮递员在响应标题它显示10,而你传球之一 –