Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android截击错误检索请求的URL_Android_Url_Error Handling_Android Volley - Fatal编程技术网

Android截击错误检索请求的URL

Android截击错误检索请求的URL,android,url,error-handling,android-volley,Android,Url,Error Handling,Android Volley,我正在使用volley库来执行一些API请求。我有一个BaseError类和一个BaseRequest类,但现在我需要在得到响应时检索URL。为了不逐个请求并在那里设置URL,我想通过VolleyError对象检索请求的URL 我如何获得我正在寻找的URL 基本错误: public abstract class BaseError implements Response.ErrorListener { private Context mContext; protected BaseError

我正在使用volley库来执行一些API请求。我有一个BaseError类和一个BaseRequest类,但现在我需要在得到响应时检索URL。为了不逐个请求并在那里设置URL,我想通过VolleyError对象检索请求的URL

我如何获得我正在寻找的URL

基本错误:

public abstract class BaseError implements Response.ErrorListener {

private Context mContext;

protected BaseError(Context context) {
    this.mContext = context;
}

@Override
public void onErrorResponse(VolleyError error) {

    String errorDesc;

    if (error.networkResponse == null) {
        if (!Connectivity.isConnected(mContext)) {
            errorDesc = mContext.getResources().getString(R.string.error_internet_connection);
        } else {
            errorDesc = mContext.getResources().getString(R.string.error_unexpected);
        }
    } else {
        try {
            if (error.networkResponse.statusCode == HttpURLConnection.HTTP_FORBIDDEN) {
                errorDesc = mContext.getResources().getString(R.string.error_authorization);
            } else {
                String dataStr = new String(error.networkResponse.data);
                JSONObject object = new JSONObject(dataStr);
                Timber.e(object.optString("message"));
                errorDesc = mContext.getResources().getString(R.string.error_unexpected);
            }
        } catch (JSONException e) {
            errorDesc = mContext.getResources().getString(R.string.error_unexpected);
        }
    }

    onError(new ErrorResponse(errorDesc, error), "url missing here...");
}

public abstract void onError(ErrorResponse response, String url);
}

我找到的答案是通过添加以下内容来更改
VolleyError
对象:

private String mUrl;

public void setUrl(String url){
    this.mUrl = url;
}

public String getUrl(){
    return mUrl;
}
而不是BaseRequest类重写方法
deliverError()

现在,在
BaseError
上,您可以访问
VolleyError
对象中的URL:

@Override
public void onErrorResponse(VolleyError error) {
    error.getUrl(); //here it is
    //....
}

或者,您可以在
onErrorResponse(截击错误)
方法中尝试此操作:

String url = error.networkResponse.headers.get("Referer");

VolleyError
附加了一个包含原始请求信息的对象。

它返回null。我没有“Referer”键。
String url = error.networkResponse.headers.get("Referer");