在Postman中获取响应代码200,但不在Android Network库上获取 [英] Getting response code 200 in Postman but not on Android Network library

查看:172
本文介绍了在Postman中获取响应代码200,但不在Android Network库上获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个POST方法API,该API在 Postman 中提供200个响应代码,但在使用 Volley 甚至在 Retrofit2 中调用api时却没有

I have a POST method API which is giving 200 response code in Postman but not when calling api by using Volley or even in Retrofit2.

这是邮递员的屏幕截图:

Here is Postman screenshots:

这是我对 Volley 库代码所做的工作:

Here what i did for Volley library code:

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请求(因为邮递员中橙色针脚设置为form-data单选按钮),而Volley是默认设置StringRequest的内容类型为application/x-www-form-urlencoded,这就是为什么会出现500错误的原因.

The problem is that your endpoint assumes multipart/form-data request (as orange pin is set to form-data radiobutton in Postman), while Volley's default content-type for StringRequest is application/x-www-form-urlencoded, that's why you get 500 error.

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

So, if you're only supposed to send POST params in multipart request, check this answer. But if you want to send files as well, check another answer

这篇关于在Postman中获取响应代码200,但不在Android Network库上获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆