BasicNetwork.performRequest:意外的响应代码400

时间:2016-02-05 08:24:06

标签: android response android-volley

我有一个大问题。通常,我没有随时收到此错误。但现在我收到了。怎么能解决这个问题?我研究并且我知道我需要使用getBodyContentType()创建自定义标头。但那不是我的解决方案..

 public void getToken(final Activity activity, final String paramUsername, final String paramPassword) {

        String tag_json_obj = "getToken";


        SharedComponent.pDialog = new ProgressDialog(activity);
        SharedComponent.pDialog.setMessage(context.getString(R.string.controlYourInfo));
        SharedComponent.pDialog.setCancelable(false);

        SharedComponent.pDialog.show();


        JSONObject params = new JSONObject();
        try {
            params.put("username", paramUsername);
            params.put("password", paramPassword);
            params.put("grant_type", "password");
        } catch (JSONException e) {
            e.printStackTrace();
        }


        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                WebUrls.getToken, params,
                new Response.Listener<JSONObject>() {


                    @Override
                    public void onResponse(JSONObject response) {
                        //     Log.d(TAG, response.toString());
                        SharedComponent.pDialog.hide();


                        try {

                            if (response.toString().contains("error")) {

                                error = response.getString("error");
                                error_description = response.getString("error_description");

                                Toast.makeText(context, error_description, Toast.LENGTH_LONG).show();

                            } else {

                                access_token = response.getString("access_token");
                                token_type = response.getString("token_type");
                                expires_in = response.getInt("expires_in");


                                SharedComponent.sharedEditor.putString("TOKEN", "Bearer " + access_token);
                                SharedComponent.sharedEditor.putString("LOGGED", "LOGGED");
                                SharedComponent.sharedEditor.commit();

                                Log.e("TOKEN", "Bearer " + access_token);

                                Intent intent = new Intent(activity, MainScreen.class);
                                context.startActivity(intent);
                                ((Activity) context).finish();

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(context,
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }


                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //   VolleyLog.d(TAG, "Error: " + error.getMessage());
                SharedComponent.pDialog.hide();

            }


        }) {

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

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

                return headers;
            }
        };

// Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    }

0 个答案:

没有答案