【问题标题】:Volley BasicNetwork.performRequest: Unexpected response code 400Volley BasicNetwork.performRequest:意外响应代码 400
【发布时间】:2016-02-08 16:42:08
【问题描述】:

我正在创建一个安卓项目。对于我的项目,我使用 PHP Slim 框架创建了一个 REST API。当我在浏览器中打开 API 的 URL 时,它工作正常。见下图。

但是,当我尝试使用 JsonObjectRequest 向此 URL(如上图所示)发送请求时,我的 logcat 中出现此错误。

E/Volley:[397] BasicNetwork.performRequest:http://192.168.94.1/MyApplication/v1/countrydata 的意外响应代码 400

我正在使用以下代码。

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Config.URL_COUNTRY_DATA, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String,String>();
            params.put("Authorization",Config.API_KEY);
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers = new HashMap<String,String>();
            headers.put("Charset", "UTF-8");
            headers.put("Content-Type", "application/x-javascript");
            return headers;
        }
    };
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);

我在谷歌上搜索了很多,发现了一些令人困惑的解决方案。有人说你没有使用标题,有人说不使用标题等。

我尝试了所有解决方案,但到目前为止都没有奏效。

【问题讨论】:

  • I am trying to send a request to this URL (shown in the snapshot above) from Android Studio.。来自安卓工作室?请解释你是如何做到的,因为我不知道这是可能的。
  • 我编辑了它.. 但是您应该通过查看代码来解决问题。 :)
  • in my browser。在哪里?这么多设备上有这么多浏览器。
  • trying to send a request to this URL。从哪里来?
  • response code 400。那么这个 http 400 是什么意思呢?

标签: php android android-volley


【解决方案1】:
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", "Bearer " + strToken);
            return headers;
        }

    };
    RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(20000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    mRequestQueue.add(jsonObjReq);

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 2015-01-03
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多