BasicNetwork.performRequest-意外的响应代码400(POST) [英] BasicNetwork.performRequest - Unexpected response code 400 (POST)

查看:61
本文介绍了BasicNetwork.performRequest-意外的响应代码400(POST)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Volley StringRequest或JsonObjectRequest通过rest api获取数据时.我总是得到400错误.它与邮递员的关系很好. Http请求方法是POST,Content-Type是application/x-www-form-urlencoded.我还检查了我的参数拼写,对我来说看起来还不错.我同时尝试了StringRequest和JsonObjectRequest,结果是E/Volley:[10283] BasicNetwork.performRequest:意外的响应代码400.以下是我的代码.我已经附上了屏幕截图.

When I am trying to fetch data through rest api using Volley StringRequest or JsonObjectRequest. I always get 400 error. Its working fine with the postman. Http request method is POST, Content-Type is application/x-www-form-urlencoded. I have also checked my parameter spelling and it looks fine to me. I have tried with both StringRequest and JsonObjectRequest, result is E/Volley: [10283] BasicNetwork.performRequest: Unexpected response code 400. Below is my code. I have attached a screenshot.

这是新的屏幕截图

StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_LOGIN, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response.toString());
                    viewHelper.hideDialog();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, error.toString());
                    viewHelper.hideDialog();
                }
            }){
                @Override
                protected Map<String, String> getParams() {
                    // Posting parameters to login url
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("grant_type", "password");
                    params.put("username", email);
                    params.put("password", password);
                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("Content-Type", "application/x-www-form-urlencoded");
                    return params;
                }

                @Override
                public String getBodyContentType(){
                    return "application/x-www-form-urlencoded";
                }
            };

            stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(stringRequest, "Authenticating User");

我已经尝试了凌空JSONObjectRequest

I have alos tried with volley JSONObjectRequest

            Map<String, String> params = new HashMap<String, String>();
            params.put("grant_type", "password");
            params.put("username", email);
            params.put("password", password);

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, AppConfig.URL_LOGIN, new JSONObject(params), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

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

任何人都可以帮助我解决此问题.

Can anyone please help me in fixing this issue.

推荐答案

下面是使用Volley库的POST请求的工作示例.
将此添加到您的build.gradle(Module:app)- 编译'com.mcxiaoke.volley:library:1.0.19'

Below is the working example of POST request using Volley library.
Add this to your build.gradle (Module:app) - compile 'com.mcxiaoke.volley:library:1.0.19'

StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_LOGIN, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response.toString());
                    progressDialog.dismiss();

                    JSONObject jsonObject = null;

                    try {
                        jsonObject = new JSONObject(response);

                        if(jsonObject.has("success")){
                            if(jsonObject.getBoolean("success") == true){
                                JSONObject userObject = new JSONObject(jsonObject.getString("user"));

                                Map<String, String> loginDetails = new HashMap<String, String>();
                                loginDetails.put(KEY_IS_LOGGED_IN, "true");
                                loginDetails.put(ACCESS_TOKEN, jsonObject.getString(ACCESS_TOKEN));
                                loginDetails.put(USERID, userObject.getString(USERID));
                                loginDetails.put(FIRSTNAME, userObject.getString(FIRSTNAME));
                                loginDetails.put(LASTNAME, userObject.getString(LASTNAME));
                                loginDetails.put(EMAIL, userObject.getString(EMAIL));

                                session = new SessionManager(_myActivity);
                                session.setLogin(loginDetails);

                                Intent intent = new Intent(LoginActivity.this, SearchActivity.class);
                                intent.putExtra("FROM_ACTIVITY", "LoginActivity");
                                startActivity(intent);
                                LoginActivity.this.finish();
                            }else{
                                btnLogin.setClickable(true);
                                btnLogin.setEnabled(true);
                                Toast.makeText(getApplicationContext(), "Username or Password does not matched!", Toast.LENGTH_LONG).show();
                            }
                        }

                    } catch (JSONException e) {
                        btnLogin.setClickable(true);
                        btnLogin.setEnabled(true);
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Something went wrong, Please try agian", Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, error.toString());
                    progressDialog.dismiss();

                    btnLogin.setClickable(true);
                    btnLogin.setEnabled(true);
                }
            }){
                @Override
                protected Map<String, String> getParams() {
                    // Posting parameters to login url
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("grant_type", "password");
                    params.put("username", email);
                    params.put("password", password);
                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("Content-Type", "application/x-www-form-urlencoded");
                    return params;
                }

                @Override
                public String getBodyContentType(){
                    return "application/x-www-form-urlencoded";
                }
            };

            stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            // Adding request to request queue
            RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());

            mRequestQueue.add(stringRequest);

如果有人遇到上述代码的问题,请发表评论.
感谢所有尝试帮助我的人.

Please comment if anyone faced issue with above code.
Thanks to everyone who tried to help me.

这篇关于BasicNetwork.performRequest-意外的响应代码400(POST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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