【问题标题】:Volley error: Unexpected response code 400Volley 错误:意外的响应代码 400
【发布时间】:2016-03-21 15:48:17
【问题描述】:

我使用下面的代码来获取 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,但这对我没有帮助。我很困惑我的代码有什么问题?

【问题讨论】:

  • 您是否尝试过从hurl.it等其他来源发送请求
  • @DennisvanOpstal 我之前在没有 Volley 的情况下使用过这项服务,效果很好
  • @DennisvanOpstal 我在 hurl.it 中测试了我的地址,它给了我 404!我厌倦了挣扎
  • 应该有404的错误信息,如果有,是什么意思

标签: android android-volley


【解决方案1】:

我不确定这是否有任何帮助,但这是我使用 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);
}

【讨论】:

  • 我的服务适用于您的代码,但不适用于本网站 hurl.it !有什么问题?
  • 你确定你所有的参数和标题都填对了吗,否则你可以从网站上检查错误,大多数时候它会说哪里出错了
  • 请查看下面的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
相关资源
最近更新 更多