gpt4 book ai didi

android - error.networkResponse 为空时 Android Volley 中的 Http 状态代码

转载 作者:IT王子 更新时间:2023-10-28 23:58:18 26 4
gpt4 key购买 nike

我在 Android 平台上使用 Google Volley。我遇到问题,其中 onErrorResponse 中的 error 参数返回 null networkResponse对于我正在使用的 RESTful API,我需要确定通常以 401 (SC_UNAUTHORIZED) 或 500 (SC_INTERNAL_SERVER_ERROR) 到达的 Http 状态代码,我偶尔可以通过以下方式检查:

final int httpStatusCode = error.networkResponse.statusCode;
if(networkResponse == HttpStatus.SC_UNAUTHORIZED) {
// Http status code 401: Unauthorized.
}

这会引发 NullPointerException,因为 networkResponse 为空。

如何确定函数onErrorResponse中的Http状态码?

或者,如何确保 error.networkResponseonErrorResponse 中不为空?

最佳答案

Or, how can I ensure error.networkResponse is non-null in onErrorResponse?

我的第一个想法是检查对象是否为空。

@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED) {
// HTTP Status Code: 401 Unauthorized
}
}

或者,您也可以尝试通过扩展 Request 来获取状态码类和覆盖 parseNetworkResponse .

例如,如果扩展摘要 Request<T>

public class GsonRequest<T> extends Request<T> {

...
private int mStatusCode;

public int getStatusCode() {
return mStatusCode;
}
...

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {

mStatusCode = response.statusCode;
try {
Log.d(TAG, "[raw json]: " + (new String(response.data)));
Gson gson = new Gson();
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(gson.fromJson(json, mClazz),
HttpHeaderParser.parseCacheHeaders(response));

} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
...
}

或者,如果您正在使用已经扩展了抽象 Request<T> 的工具箱类之一类,你不想混淆 parseNetworkResponse(NetworkResponse networkResponse) 的实现, 继续覆盖该方法但通过 super.parseNetworkResponse(networkResponse) 返回 super 的实现

例如StringResponse

public class MyStringRequest extends StringRequest {

private int mStatusCode;

public MyStringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, listener, errorListener);
}

public int getStatusCode() {
return mStatusCode;
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
mStatusCode = response.statusCode;
return super.parseNetworkResponse(response);
}
}

用法:

public class myClazz extends FragmentActivity {


private Request mMyRequest;
...

public void makeNetworkCall() {
mMyRequest = new MyNetworkRequest(
Method.GET,
BASE_URL + Endpoint.USER,
new Listener<String>() {

@Override
public void onResponse(String response) {
// Success

}
},
new ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
if (mMyRequest.getStatusCode() == 401) {
// HTTP Status Code: 401 Unauthorized
}
}
});

MyVolley.getRequestQueue().add(request);
}

当然,覆盖内联方法的选项也是可用的

public class MyClazz extends FragmentActivity {

private int mStatusCode;

...

public void makeNetworkCall() {

StringRequest request = new StringRequest(
Method.GET,
BASE_URL + Endpoint.USER,
new Listener<String>() {

@Override
public void onResponse(String response) {
// Success

}
},
new ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
if (mStatusCode == 401) {
// HTTP Status Code: 401 Unauthorized
}
}
}) {

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
mStatusCode = response.statusCode;
return super.parseNetworkResponse(response);
}
};
MyVolley.getRequestQueue.add(request);
}

更新:
HttpStatus已弃用。使用HttpURLConnection反而。见 Link .

关于android - error.networkResponse 为空时 Android Volley 中的 Http 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22948006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com