【问题标题】:Getting Volley.ServerError while making a JSONObjectRequest to an IP with a PORT向带有 PORT 的 IP 发出 JSONObjectRequest 时出现 Volley.ServerError
【发布时间】:2017-11-01 12:39:11
【问题描述】:

我看过很多博客和链接,但找不到满意的答案。我正在我的应用程序中使用 volley 发出一个简单的 POST 请求 (JSONObjectRequest),但在 volley 错误中得到 com.android.volley.ServerError。我使用的 URL 是“http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register”。凌空是否有可能不支持其中包含端口号的网址?需要一个简单而精确的解决方案。

代码:

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)

【问题讨论】:

  • 我们需要您简单准确的相关代码和错误的完整 logcat 堆栈跟踪
  • 能否请您添加更多详细信息
  • 更新了我的帖子
  • 您还可以从以下网址发布示例回复:"http://192.XXX.X.XX:8080/pushnotificaitons_staging/ 吗?
  • 400 意味着通常意味着错误的请求。您是否在请求中缺少某些参数。首先在邮递员上尝试您的 api,无论它是否有效

标签: android android-volley jsonobjectrequest


【解决方案1】:

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 标头设置为application/json; charset=utf-8。您所做的是还为content-type 发送另一个标头,这导致服务器响应状态为400。通过删除该行来删除额外的标题:

header.put("Content-Type", ApplicationConstants.Content_Type);

请求仅包含正确的默认标头。

【讨论】:

  • 虽然我确实有其他 API,我在其中发送带有 JSONObjectRequest 的标头中的内容类型并且它们正在工作,但为什么这不起作用?
  • 我不知道您的应用程序,也许您的 api 服务器端代码接受这些标头,http 状态 400 表示服务器无法理解请求,所以我想这取决于服务器端代码要做什么接受为有效请求或不接受。对于特定情况,您设置的自定义标头不是您的服务器所期望的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2019-11-17
  • 2015-10-07
  • 2015-10-05
相关资源
最近更新 更多