在邮递员中获取响应代码200但在Android网络库中没有

Vik*_*uli 2 android internal-server-error android-volley retrofit2

我有一个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));
Run Code Online (Sandbox Code Playgroud)

错误Logcat:

BasicNetwork.performRequest: Unexpected response code 500 for http://xxxxxxxxxxx.com/api/mobile/user/post/
Run Code Online (Sandbox Code Playgroud)

rom*_*4ek 7

问题是你的端点假定multipart/form-data请求(因为橙色引脚设置为form-dataPostman中的单选按钮),而Volley默认内容类型为StringRequestis application/x-www-form-urlencoded,这就是你得到500错误的原因.

因此,如果您只是在多部分请求中发送POST参数,请检查此答案.但是如果你想发送文件,请检查另一个答案