使用Volley获取成功请求的HTTP状态代码

我正在检索一个无效url的内容,比如http://www.gigd32fdsu.com :这是我的testing代码:

 // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); final String url = "http://www.gigd32fdsu.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener() { @Override public void onResponse(Object response) { // Display the first 500 characters of the response string. mTextView.setText("Response is: " + response.toString().substring(0, 500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mTextView.setText("That didn't work! " + error.networkResponse.statusCode); } }); // Add the request to the RequestQueue. queue.add(stringRequest); 

当我运行这个代码时,我收到来自ISP的错误页面的callbackonResponse(String)。 如何读取HTTP状态码以检测网页显示不正确?

谢谢

Related of "使用Volley获取成功请求的HTTP状态代码"

简单的解决scheme是重写makeStringReq()中的parseNetworkResponse,不需要另一个类:

 private void makeStringReq() { showProgressDialog(); StringRequest strReq = new StringRequest(Method.GET, Const.URL_STRING_REQ, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(TAG, response.toString()); msgResponse.setText(response.toString()); hideProgressDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); hideProgressDialog(); } }) { @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { int mStatusCode = response.statusCode; return super.parseNetworkResponse(response); } }; // Adding request to request queue AppController.getInstance().addToRequestQueue(strReq, tag_string_req); } 

只需重写parseNetworkResponse方法,然后取得statusCode值。

 public class StrImplRequest extends StringRequest { @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { // take the statusCode here. response.statusCode; return super.parseNetworkResponse(response); } } 

我会让VinceStyling的回应更完整。 我会告诉你我做了什么

一旦你重写了这个方法,在你的类中保存statusCode。

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

之后,您应该将其与HttpURLConnection常量进行比较以采取相应的行动。 例如:

  int statusCode=webService.getStatusCode(); switch (statusCode){ case HttpURLConnection.HTTP_OK: //do stuff break; case HttpURLConnection.HTTP_NOT_FOUND: //do stuff break; case HttpURLConnection.HTTP_INTERNAL_ERROR: //do stuff break; } 

@VinceStyling的答案是好的,或者你可以扩展Request类,做你想做的事情。 例如,

  ServerStatusRequestObject extends Request { private final Response.Listener mListener; private String mBody = ""; private String mContentType; public ServerStatusRequestObject(int method, String url, Response.Listener listener, Response.ErrorListener errorListener) { super(method, url, errorListener); mListener = listener; mContentType = "application/json"; if (method == Method.POST) { RetryPolicy policy = new DefaultRetryPolicy(5000, 0, 5); setRetryPolicy(policy); } } @Override protected Response parseNetworkResponse(NetworkResponse response) { return Response.success(response.statusCode, HttpHeaderParser.parseCacheHeaders(response)); } @Override protected void deliverResponse(Object response) { if (mListener != null) { mListener.onResponse(response); } } @Override public byte[] getBody() throws AuthFailureError { return mBody.getBytes(); } @Override public String getBodyContentType() { return mContentType; } @Override public int compareTo(Object another) { return 0; } 

那么在您的响应处理程序中,您仍然可以从服务器接收整个消息。 尝试一下。