【发布时间】:2015-12-14 08:41:12
【问题描述】:
我正在使用 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);
}
【问题讨论】:
标签: android url error-handling android-volley