在 Postman 中获取响应代码 200,但在 Android 网络库中却没有
Getting response code 200 in Postman but not on Android Network library
我有一个 POST 方法 API,它在 Postman 中给出了 200 响应代码,但在使用 [= 调用 api 时却没有24=]Volley 甚至 Retrofit2.
这是 Postman 截图:
这是我为 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/
问题是您的端点假定 multipart/form-data
请求(因为橙色图钉在 Postman 中设置为 form-data
单选按钮),而 Volley
的默认 content-type StringRequest
是 application/x-www-form-urlencoded
,这就是你得到 500 错误的原因。
因此,如果您只应在多部分请求中发送 POST 参数,请选中 this answer. But if you want to send files as well, check another answer
你为什么不使用 JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, JSONObject, response, error);
post 请求它很容易使用试一次
我也遇到了同样的问题,我使用
解决了
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
我有一个 POST 方法 API,它在 Postman 中给出了 200 响应代码,但在使用 [= 调用 api 时却没有24=]Volley 甚至 Retrofit2.
这是 Postman 截图:
这是我为 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/
问题是您的端点假定 multipart/form-data
请求(因为橙色图钉在 Postman 中设置为 form-data
单选按钮),而 Volley
的默认 content-type StringRequest
是 application/x-www-form-urlencoded
,这就是你得到 500 错误的原因。
因此,如果您只应在多部分请求中发送 POST 参数,请选中 this answer. But if you want to send files as well, check another answer
你为什么不使用 JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, JSONObject, response, error);
post 请求它很容易使用试一次
我也遇到了同样的问题,我使用
解决了 @Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}