【发布时间】:2017-01-30 12:07:27
【问题描述】:
我有一个 POST 方法 API,它在 Postman 中提供 200 个响应代码,但在使用 Volley 甚至在 Retrofit2 中调用 api 时却没有.
这是邮递员的截图:
这是我为 Volley 库代码所做的:
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/
【问题讨论】:
-
你能分享你的清单吗?您是否添加了以下权限? "android.permission.INTERNET" "android.permission.ACCESS_NETWORK_STATE"
-
毫无疑问。
-
500 是服务器错误。我对这些情况的经验通常与标题有关。 Postman 默认添加了一些改造和凌空没有的标头。有时服务器无法应对这种缺乏标题并抛出a500。我建议你检查一下。也可能反过来。
-
您可以从邮递员那里获取工作代码,因为它在保存按钮下提供
-
实际上不知道你为什么忽略我的回答,似乎它解决了问题。
标签: android android-volley retrofit2 internal-server-error