【发布时间】:2016-08-10 03:18:56
【问题描述】:
我正在开发一个 Android 应用程序。在我的应用程序中,我正在使用 Android 连接到服务器。我在服务器端使用 Laravel。我的服务器端代码运行良好,因为我已经对其进行了测试。问题在于检查 Volley 错误侦听器中的错误状态代码。当我检查状态码时它总是为零,即使当我在浏览器中检查并使用其他工具时它返回 401。
这是我的 Volley 请求返回,从服务器抛出错误(401 状态):
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("STATUS_CODE",CommonHelper.volleyResponseStatusCode(error));
if (CommonHelper.volleyResponseStatusCode(error) == 401) {
startActivity(new Intent(getActivity(), LoginActivity.class));
} else {
showMessageDialog("Server error encountered");
}
}
});
如您所见,我记录了状态代码。
这是我的 CommonHelper 类,带有检查 Volley 状态代码的方法
public final class CommonHelper {
.
.
.
public static int volleyResponseStatusCode(VolleyError error)
{
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
return networkResponse.statusCode;
}
else{
return 0;
}
}
}
每当我检查状态码时,它总是返回 0。这是为什么,如何正确检查?
【问题讨论】:
-
嘿,你为什么不先尝试使用邮递员或任何其他客户端来检查服务器是否在失败时发送这样的响应?
-
我已经尝试过使用其他工具。响应是 401 状态码。我检查了多种工具。
-
如果您的请求超时,您可能需要通过设置重试策略来检查它。
-
没有没有。请求没有超时。它会立即响应。
-
好的。您可以尝试使用 StringRequest 而不是 JSONObjectRequest 吗?