安卓。 Volley 。 volleyError.networkResponse 为空

标签 android android-volley

我正在使用 Volley 调用网络服务 所以……我正在打这个电话:

POST /api/user/login HTTP/1.1
Content-Type: application/json
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.2.2; Samsung Galaxy S3 - 4.2.2 - API 17 - 720x1280 Build/JDQ39E)
Host: /*No need to show this here.*/
Connection: Keep-Alive
Accept-Encoding: gzip
Content-Length: 43

{"password":"sg","email":"string1@str.com"}

我收到了这个响应(注意 401 错误):

HTTP/1.1 401 Unauthorized
Server: nginx/1.6.3
Date: Thu, 06 Aug 2015 17:39:15 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.27

{"error":"User is not activated"}

我的请求对象如下所示:

public class CustomJsonObjectRequest extends JsonRequest<JSONObject> {

    private Class responseType = null;

    public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<org.json.JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse resp) {
        return null; //For the purpose of this question, let's presume it's null. It doesn't even enter here, so no use considering what's in here
    }

    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError) {

        /** IF  I SET A BREAKPOINT ANYWHERE IN HERE ... volleyError.networkResponse WILL BE NULL !!! WHY ?! You can ignore what's below ... it's not really important*/

        JSONObject jsonResp = null;
        String message = "Could not connect to the server.";// default response
        if (volleyError != null && volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
            try {
                /* get the object */
                jsonResp = new JSONObject(new String(volleyError.networkResponse.data));
                message = jsonResp.getString("detail");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            UDebug.log("PARSED NETWORK ERROR: [" + new VolleyError(message) + "]");
            return new VolleyError(message);
        }
        return new VolleyError(message);
    }

}

它应该进入 parseNetworkError,但为什么 volleyError.networkResponse 为空? volleyError.networkResponse.data 应包含字符串 {"error":"User is not activated"}。为什么这没有发生?有什么想法吗?

如果您需要一些额外的代码或说明,请告诉我...

最佳答案

所以我在 4.3 之前的设备上测试过它,似乎它在 4.3 之后的设备上不起作用。确实如此。

我已经要求后端人员将错误响应从 401 更改为 403,现在看来可以了。我在某处读到 401 的 header 可能是错误的或可能不包含适当的数据,这使代码变得困惑。

我不知 Prop 体的原因。我们将其保留为 403 错误,一切正常。

关于安卓。 Volley 。 volleyError.networkResponse 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31862785/

相关文章:

php - android 从 mysql 中检索数据并将其呈现在 recycleview 中并添加事件

java - Android volley图片缓存问题

java - 如何正确设置匿名类中的字段并在主类中使用它们?

android - 如何在 Toast 通知中包含按钮?

android - 如何使用Proguard缩小Android代码

android - 为什么我在 eclipse 中看不到我的布局文件

php - 无法登录 Android 应用程序。错误 : org. json.JSONException : Value <br of type java. lang.String 无法转换为 JSONArray

Android:听所有 toast

android - Jetpack compose AppBarIcon 提示 "Functions which invoke @Composable functions must be marked with the @Composable"

android - 无法分配 JNI Env