Volley Content-Type在某些手机上没有变化

时间:2017-09-07 19:53:51

标签: android api android-volley content-type

我正在使用volley和JsonArrayAuthRequest来获取api。 api适用于我的一些手机,但它失败了另一个人(两者都使用api level 23,marshmellow)

他们得到的错误信息是" 400"请求形成不良。它显示了Content =" text / html; charset = us-ascii"。但是,我知道预期的实体是json / application。

我尝试重写getHeaders()和getBodyContentType()..有什么想法吗?

    String baseUrl = Constants.constants.getApiUrl() +"Events/";
    String url = baseUrl + query;
    //Log.i("url",url);
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    JsonArrayAuthRequest eventRequest = new JsonArrayAuthRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    ObjectMapper mapper = new ObjectMapper()
                            .disable(FAIL_ON_UNKNOWN_PROPERTIES); // fails if not 1 to 1 mapping
                    ArrayList<Event> data = new ArrayList<>();
                    for (int i = 0; i < response.length(); ++i) {
                        Object res;
                        try {
                            res = response.get(i);
                            //Log.i("res", res.toString()); //grabs the filtered events already
                            Event e = mapper.readValue(res.toString(), Event.class);
                            data.add(e);
                        } catch (Exception e) {
                            Log.d("error", e.toString());
                            return;
                        }
                    }
                    //Log.i("Browse", "got " + Integer.toString(data.size()) + " events!");

                    browse = (BrowseFragment) adapter.getItem(0);
                    browse.setData(data);
                    progressDialog.dismiss();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error == null || error.networkResponse == null) {
                        Toast toast = Toast.makeText(getApplicationContext(), "No Response from server", Toast.LENGTH_LONG);
                        toast.show();
                        return;
                    }
                    String errorMessage = "Could not stringify errorMessage";
                    try {
                         errorMessage = new String(error.networkResponse.data,"UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    Toast toast = Toast.makeText(getApplicationContext(), error.networkResponse.statusCode + ": " + errorMessage, Toast.LENGTH_LONG);
                    toast.show();

                    progressDialog.dismiss();

                }
            });
    queue.add(eventRequest)
 }


private class JsonArrayAuthRequest extends JsonArrayRequest {
        JsonArrayAuthRequest(String url, Response.Listener<JSONArray> 
listener, Response.ErrorListener errorListener) {
            super(url, listener, errorListener);
        }

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

            SharedPreferences settings = getSharedPreferences("account", MODE_PRIVATE);
            headers.put("Authorization", UserInfo.User.getToken());
            //Log.i("headers",headers.get("Content-Type") + " " + headers.get("Authorization"));
            return headers;
        }
        @Override
        public String getBodyContentType()
        {
            return "application/json; charset=utf-8";
        }
    }

1 个答案:

答案 0 :(得分:0)

这不是内容类型错误。真正的错误是api url包含一个导致400错误的空间。我的手机必须缓存以前的网址?不确定..但我一直在寻找这个解决方案的错误方向..