使用 PORT 向 IP 发出 JSONObjectRequest 时获取 Volley.ServerError
Getting Volley.ServerError while making a JSONObjectRequest to an IP with a PORT
我看过很多博客和链接,但找不到满意的答案。我在我的应用程序中使用凌空制作一个简单的 POST 请求 (JSONObjectRequest) 但得到 com.android.volley.ServerError凌空抽射失误。我使用的 URL 是“http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register”。 volley 是否有可能不支持其中包含端口号的 url?需要一个简单而精确的解决方案。
代码:
Map<String, String> body = new HashMap<>();
body.put("deviceId", "STECH-1502878253");
body.put("token", "XYZ");
body.put("deviceType", "IPHONE");
body.put("companyKey", "STECH");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,"http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register", new JSONObject(body), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("Data response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VolleyError","Error response", error);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Content-Type", ApplicationConstants.Content_Type);
header.put("Authorization", ApplicationConstants.CUSTOM_AUTH_TOKEN);
return header;
}
};
requestQueue.add(request);
堆栈跟踪:
E/Volley: [35860] BasicNetwork.performRequest: Unexpected response code 400 for http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register
W/System.err: com.android.volley.ServerError
W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
JsonObjectRequest
扩展 JsonRequest
。如果您查看 JsonRequest
的源代码,您会发现:
/** Default charset for JSON request. */
protected static final String PROTOCOL_CHARSET = "utf-8";
/** Content type for request. */
private static final String PROTOCOL_CONTENT_TYPE =
String.format("application/json; charset=%s", PROTOCOL_CHARSET);
@Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
所以 JsonObjectRequest
通过 默认值 将 content-type
header 设置为 application/json; charset=utf-8
。您所做的是还为 content-type
发送了另一个 header,这导致服务器响应状态为 400
。通过删除行删除额外的 header:
header.put("Content-Type", ApplicationConstants.Content_Type);
请求仅包括默认值 header,这是正确的。
我看过很多博客和链接,但找不到满意的答案。我在我的应用程序中使用凌空制作一个简单的 POST 请求 (JSONObjectRequest) 但得到 com.android.volley.ServerError凌空抽射失误。我使用的 URL 是“http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register”。 volley 是否有可能不支持其中包含端口号的 url?需要一个简单而精确的解决方案。
代码:
Map<String, String> body = new HashMap<>();
body.put("deviceId", "STECH-1502878253");
body.put("token", "XYZ");
body.put("deviceType", "IPHONE");
body.put("companyKey", "STECH");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,"http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register", new JSONObject(body), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("Data response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VolleyError","Error response", error);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Content-Type", ApplicationConstants.Content_Type);
header.put("Authorization", ApplicationConstants.CUSTOM_AUTH_TOKEN);
return header;
}
};
requestQueue.add(request);
堆栈跟踪:
E/Volley: [35860] BasicNetwork.performRequest: Unexpected response code 400 for http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register
W/System.err: com.android.volley.ServerError
W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
JsonObjectRequest
扩展 JsonRequest
。如果您查看 JsonRequest
的源代码,您会发现:
/** Default charset for JSON request. */
protected static final String PROTOCOL_CHARSET = "utf-8";
/** Content type for request. */
private static final String PROTOCOL_CONTENT_TYPE =
String.format("application/json; charset=%s", PROTOCOL_CHARSET);
@Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
所以 JsonObjectRequest
通过 默认值 将 content-type
header 设置为 application/json; charset=utf-8
。您所做的是还为 content-type
发送了另一个 header,这导致服务器响应状态为 400
。通过删除行删除额外的 header:
header.put("Content-Type", ApplicationConstants.Content_Type);
请求仅包括默认值 header,这是正确的。