排球错误:意外的响应代码400

时间:2016-03-21 15:48:18

标签: android android-volley

我使用下面的代码来获取Volley请求并获取json:

  JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.POST,
                service_address , null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                VolleyLog.d(TAG, "Response: " + response.toString());
                if (response != null) {
                    parseJsonFeed(response);
                }
            }
        }, new Response.ErrorListener() {

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


            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put("username", username);
                params.put("password", password);

                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                // Removed this line if you dont need it or Use application/json
                // params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };

但在运行之后,我在logcat

中给出了这个错误
BasicNetwork.performRequest: Unexpected response code 400

我看了this link,但这对我没有帮助。我对代码的错误感到困惑?

1 个答案:

答案 0 :(得分:2)

我不确定这是否有任何帮助,但以下是我如何将Volley用于我的登录请求:

/**
 * Method that checks given user credentials with the ones in the online DB
 * @param v the view that activated this method
 */
public void logIn(View v) {
    final String email = String.valueOf(((EditText) findViewById(R.id.inputEmail)).getText());
    final String password = String.valueOf(((EditText) findViewById(R.id.inputPassword)).getText());

    String tag_string_req = "string_req";
    final String TAG = AppController.class
            .getSimpleName();
    String url = "http://android.diggin.io/diggin/v1/login";
    StringRequest strReq = new StringRequest(Request.Method.POST,
            url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, response);
            try {
                final JSONObject jsonObject = new JSONObject(response);
                if (!jsonObject.getBoolean("error")) {
                    apiKey = jsonObject.getString("apiKey");
                    SharedPreferences sharedPref = getSharedPreferences(getString(R.string.apiKey), Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putString(getString(R.string.apiKey), apiKey);
                    editor.commit();
                    refreshApiKey();
                } else {
                    //Send message when something goes wrong
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            AlertDialog.Builder dlgAlert = new AlertDialog.Builder(LoginActivity.this);
                            try {
                                dlgAlert.setMessage(jsonObject.getString("message"));
                            } catch (JSONException e) {
                                dlgAlert.setMessage("Something went wrong, please try again");
                            }
                            dlgAlert.setPositiveButton("OK", null);
                            dlgAlert.setCancelable(true);
                            dlgAlert.create().show();
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            //Send message when something goes wrong
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(LoginActivity.this);
                    dlgAlert.setMessage("Error while logging in, please try again");
                    dlgAlert.setPositiveButton("OK", null);
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();
                }
            });
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("email", email);
            params.put("password", password);

            return params;
        }
    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}