Prase 401 volley error message

依然范特西╮ 提交于 2019-12-25 14:25:54

问题


How to parse 401 response data for volley,here is the server response am getting i haev used volley network response but its not able to get response data which is in JSON format

Status 200 OK 
{
    alert: {
        title: "Unauthorised access to appointment"
        message: ""
    }-
    response: "0"
}



new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Req Failed!");
        mProgressDialog.dismiss();
        NetworkResponse localNetworkResponse = error.networkResponse;
        if (localNetworkResponse != null) {
            if (!WebAPIEngine.checkStatusCodeVolley(localNetworkResponse.statusCode)) {
                AlertDialogUtil.displayErrorAlert(error.toString(), mContext);
            }
        } else {
            Log.e(TAG, error.toString());
            try {
                JSONObject jsonObject = new JSONObject(error.getMessage());
                    if (WebAPIEngine.processErrorAlertResp(jsonObject)) {
                        AlertDialogUtil.displayErrorAlert(WebAPIEngine.getErrorMessage(), mContext);
                    } else {
                        AlertDialogUtil.displayErrorAlert(error.toString(), mContext);
                    }
                } 
            catch (Exception e) { e.printStackTrace(); }
        }
    }
});

回答1:


You can use the following code to get response

String responseString="";
if (networkResponse != null) {
    try {
        Log.v(TAG , ".getResponseError code :"+networkResponse.statusCode);
        responseString = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
        Log.v(TAG, ".getResponseError body :" + responseString);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

Also, beware 401 errors are unreliable to handle in Volley: The SO answer explains it perfectly.




回答2:


UPDATE July 2016

As vedant1811 mentioned, 401 errors are unreliable to handle in Volley but I recently found a workaround not mentioned in The S.O answer and I thought it could be useful if the provided solutions does not suit your case.

1) Create a base request extending any volley request (StringRequest, JSONObjectRequest, etc...) and override parseNetworkError() function

/* Custom volley request. I chose to extend JsonObjectRequest
 * feel free to extend any other volley class
 */ 
public class CundinasRequest extends JsonObjectRequest{
...
  @Override
  //This will make the volley error message to contain your server's error message
  protected VolleyError parseNetworkError(VolleyError volleyError) {
      if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
          volleyError = new VolleyError(new String(volleyError.networkResponse.data));
      }
      return volleyError;
  }
...
}

2) In your onErrorResponse() implementation you just have to parse your server's error. Here's an example:

//Volley request is made somewhere in this activity
public class MainActivity extends Activity {
  private final String AUTH_ERROR_IDENTIFIER = "invalid_token";
...
  public isAuthorizationError(VolleyError error){
    return error.getMessage().contains(AUTH_ERROR_IDENTIFIER); 
  }

  @Override
    public void onErrorResponse(VolleyError error) {
      if(isAuthorizationError(error)
        Log.e(TAG, "Please log in again");
  }

}      

I found this workaround useful as I couldn't change the server's return status and the authorization error was always the same. To give you an idea, it looked something like this:

{
  "error": "invalid_token",
  "error_description": "Invalid access token: AN INVALID TOKEN"
}


来源:https://stackoverflow.com/questions/26927047/prase-401-volley-error-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!