【问题标题】:Getting response code 200 in Postman but not on Android Network library在 Postman 中获取响应代码 200 但在 Android 网络库中没有
【发布时间】: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


【解决方案1】:

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

因此,如果您只应该在多部分请求中发送 POST 参数,请检查 this answer。但如果你也想发送文件,请查看another answer

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这不是库问题,它的标头是您通过的,您可以使用 ION 网络库检查 android 或在 Postman 中检查响应标头,它在您通过时显示 10
    【解决方案3】:

    我也遇到了同样的问题,我通过使用解决了

     @Override
     public Map<String, String> getHeaders() throws AuthFailureError {
      final Map<String, String> headers = new HashMap<>();
      headers.put("Content-Type", "application/json");
      return headers;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 2019-08-23
      • 2018-07-24
      • 2016-09-20
      • 2021-01-02
      • 1970-01-01
      相关资源
      最近更新 更多