Android VolleyError检索请求的URL [英] Android VolleyError retrieve requested URL
问题描述
我正在使用排球库执行一些API请求.我有一个BaseError类和一个BaseRequest类,但是现在当我得到响应时,我需要检索URL.为了不按请求进行请求并在那里设置URL,我想通过VolleyError对象检索所请求的URL.
I am using volley library to do some API request. I have a BaseError class and a BaseRequest class, but now i need to retrieve the URL, when i get a response. To not go request by request and set the URL there, I would like to retrieve the requested URL by VolleyError object.
如何获取我要查找的URL?
How can i get the URL I am looking for?
BaseError:
BaseError:
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
对象:
The answer i found for this was to change the VolleyError
object, by adding:
private String mUrl;
public void setUrl(String url){
this.mUrl = url;
}
public String getUrl(){
return mUrl;
}
比BaseRequest类重写方法deliverError()
:
Than on BaseRequest class override the method deliverError()
:
@Override
public void deliverError(VolleyError error) {
error.setUrl(getUrl());
listenerError.onErrorResponse(error);
}
现在在BaseError
上,您可以访问VolleyError
对象内的URL:
Now on BaseError
you have access to the URL inside the VolleyError
object:
@Override
public void onErrorResponse(VolleyError error) {
error.getUrl(); //here it is
//....
}
这篇关于Android VolleyError检索请求的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!