Android Volley - BasicNetwork.performRequest:意外响应代码 400 [英] Android Volley - BasicNetwork.performRequest: Unexpected response code 400

查看:28
本文介绍了Android Volley - BasicNetwork.performRequest:意外响应代码 400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述:

我正在尝试访问一个 REST API,该 API 将使用 Volley 返回各种 HTTP 状态代码(400、403、200 等)的 JSON 对象.

I am trying to access an REST API that will return a JSON object for various HTTP status codes (400, 403, 200 etc) using Volley.

对于除 200 以外的任何 HTTP 状态,意外响应代码 400"似乎是一个问题.有没有人有办法绕过这个错误"?

For any HTTP status other than 200, it seems the 'Unexpected response code 400' is a problem. Does anyone have a way to bypass this 'error'?

代码:

protected void getLogin() {   
    final String mURL = "https://somesite.com/api/login";

    EditText username = (EditText) findViewById(R.id.username);
    EditText password = (EditText) findViewById(R.id.password);

    // Post params to be sent to the server
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("username", username.getText().toString());
    params.put("password", password.getText().toString());

    JsonObjectRequest req = new JsonObjectRequest(mURL, new JSONObject(
            params), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONObject obj = response
                        .getJSONObject("some_json_obj");

                Log.w("myApp",
                        "status code..." + obj.getString("name"));

                // VolleyLog.v("Response:%n %s", response.toString(4));

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.w("error in response", "Error: " + error.getMessage());
        }
    });

    // add the request object to the queue to be executed
    AppController.getInstance().addToRequestQueue(req);
}

推荐答案

在不更改 Volley 源代码的情况下执行此操作的一种方法是检查 VolleyError<中的响应数据/code> 并自行解析.

One way of doing this without changing Volley's source code is to check for the response data in the VolleyError and parse it your self.

截至 f605da3 commitVolley 抛出一个 ServerError 异常 包含原始网络响应.

As of f605da3 commit, Volley throws a ServerError exception that contains the raw network response.

所以你可以在你的错误监听器中做类似的事情:

So you can do something similar to this in your error listener:

/* import com.android.volley.toolbox.HttpHeaderParser; */
public void onErrorResponse(VolleyError error) {

    // As of f605da3 the following should work
    NetworkResponse response = error.networkResponse;
    if (error instanceof ServerError && response != null) {
        try {
            String res = new String(response.data,
                       HttpHeaderParser.parseCharset(response.headers, "utf-8"));
            // Now you can use any deserializer to make sense of data
            JSONObject obj = new JSONObject(res);
        } catch (UnsupportedEncodingException e1) {
            // Couldn't properly decode data to string
            e1.printStackTrace();
        } catch (JSONException e2) {
            // returned data is not JSONObject?
            e2.printStackTrace();
        }
    }
}

以后,如果Volley发生变化,可以按照上面的方法检查服务器发送的原始数据的VolleyError并解析.

For future, if Volley changes, one can follow the above approach where you need to check the VolleyError for raw data that has been sent by the server and parse it.

我希望他们实现源文件.

I hope that they implement that TODO mentioned in the source file.

这篇关于Android Volley - BasicNetwork.performRequest:意外响应代码 400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆