下面列出了怎么用android.net.ParseException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t.getMessage());
//这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
//这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
@Override
public void onError(Throwable t) {
//这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
//这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
// ArmsUtils.snackbarText(msg);
Log.e("TAG", msg);
Log.e("============", t.getMessage());
}
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t.getMessage());
//这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
//这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
@Override
public void onError(Throwable e) {
new Handler().postDelayed(()->{
if(isShowErrorView) baseView.showErrorView();
},500);
e.printStackTrace();
if (e instanceof UnknownHostException) {
Log.e(TAG_ERROR, "networkError:" + e.getMessage());
networkError();
} else if (e instanceof InterruptedException) {
Log.e(TAG_ERROR, "timeout:" + e.getMessage());
timeoutError();
} else if (e instanceof HttpException) {
Log.e(TAG_ERROR, "http错误:" + e.getMessage());
httpError();
} else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
Log.e(TAG_ERROR, "解析错误:" + e.getMessage());
parseError();
}else {
Log.e(TAG_ERROR, "未知错误:" + e.getMessage());
unknown();
}
}
@Override
public void handleResponseError(Context context, Throwable t) {
//用来提供处理所有错误的监听
//rxjava必要要使用ErrorHandleSubscriber(默认实现Subscriber的onError方法),此监听才生效
Timber.tag("Catch-Error").w(t.getMessage());
//这里不光是只能打印错误,还可以根据不同的错误作出不同的逻辑处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException) {
msg = "数据解析错误";
}
UiUtils.snackbarText(msg);
}
@Override
public Uri getReferrer() {
// There is a built in function available from SDK>=22
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
return super.getReferrer();
}
Intent intent = getIntent();
Uri referrer = (Uri) intent.getExtras().get("android.intent.extra.REFERRER");
if (referrer != null) {
return referrer;
}
String referrerName = intent.getStringExtra("android.intent.extra.REFERRER_NAME");
if (referrerName != null) {
try {
return Uri.parse(referrerName);
} catch (ParseException e) {
// ...
}
}
return null;
}
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t);
//这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
//这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
static String gofordate(String s) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(s);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
System.out.println("date" + date + " weekDays " + weekDays[w]);
return weekDays[w];
}
/**
* Cleans up (if possible) user-entered web addresses
*/
public static String guessUrl(String inUrl) {
String retVal = inUrl;
WebAddress webAddress;
if (TRACE) Log.v(LOGTAG, "guessURL before queueRequest: " + inUrl);
if (inUrl.length() == 0) return inUrl;
if (inUrl.startsWith("about:")) return inUrl;
// Do not try to interpret data scheme URLs
if (inUrl.startsWith("data:")) return inUrl;
// Do not try to interpret file scheme URLs
if (inUrl.startsWith("file:")) return inUrl;
// Do not try to interpret javascript scheme URLs
if (inUrl.startsWith("javascript:")) return inUrl;
// bug 762454: strip period off end of url
if (inUrl.endsWith(".") == true) {
inUrl = inUrl.substring(0, inUrl.length() - 1);
}
try {
webAddress = new WebAddress(inUrl);
} catch (ParseException ex) {
if (TRACE) {
Log.v(LOGTAG, "smartUrlFilter: failed to parse url = " + inUrl);
}
return retVal;
}
// Check host
if (webAddress.getHost().indexOf('.') == -1) {
// no dot: user probably entered a bare domain. try .com
webAddress.setHost("www." + webAddress.getHost() + ".com");
}
return webAddress.toString();
}
static ResponseError handleError(Throwable e) {
ResponseError error;
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
return handleHttpError(exception);
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException || e instanceof MalformedJsonException) {
error = new ResponseError(e, ERROR.PARSE_ERROR);
error.message = "解析响应数据错误";
return error;
} else if (e instanceof ConnectException) {
error = new ResponseError(e, ERROR.NETWORK_ERROR);
error.message = "连接失败,请重试";
return error;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
error = new ResponseError(e, ERROR.SSL_ERROR);
error.message = "证书验证失败";
return error;
} else if (e instanceof ConnectTimeoutException) {
error = new ResponseError(e, ERROR.TIMEOUT_ERROR);
error.message = "连接超时,请重试";
return error;
} else if (e instanceof java.net.SocketTimeoutException) {
error = new ResponseError(e, ERROR.TIMEOUT_ERROR);
error.message = "请求超时,请重试";
return error;
} else {
error = new ResponseError(e, ERROR.UNKNOWN);
error.message = e.getMessage();
return error;
}
}
public static String getExceptionMessage(Throwable throwable) {
String message;
if (throwable instanceof ApiException) {
message = throwable.getMessage();
} else if (throwable instanceof SocketTimeoutException) {
message = "网络连接超时,请稍后再试";
} else if (throwable instanceof ConnectException) {
message = "网络连接失败,请稍后再试";
} else if (throwable instanceof HttpException||throwable instanceof retrofit2.HttpException) {
message = "网络出错,请稍后再试";
} else if (throwable instanceof UnknownHostException || throwable instanceof NetNotConnectedException) {
message = "当前无网络,请检查网络设置";
} else if (throwable instanceof SecurityException) {
message = "系统权限不足";
} else if (throwable instanceof JsonParseException
|| throwable instanceof JSONException
|| throwable instanceof ParseException) {
message = "数据解析错误";
} else if (throwable instanceof javax.net.ssl.SSLHandshakeException) {
message = "网络证书验证失败";
} else {
message = throwable.getMessage();
if (message==null||message.length() <= 40) {
message = "出错了 ≥﹏≤ ,请稍后再试";
}
}
return message;
}
public static ApiException handleException(Throwable e) {
//使用RxCache之后返回的是包裹的CompositeException,一般包含2个异常,rxcache异常和原本的异常
Logger.t(TAG).d("开始解析错误------");
if (e instanceof CompositeException) {
CompositeException compositeException = (CompositeException) e;
for (Throwable throwable : compositeException.getExceptions()) {
if (!(throwable instanceof RxCacheException)) {
e = throwable;
Logger.t(TAG).d("其他异常:" + throwable.getMessage());
} else {
Logger.t(TAG).d("RxCache 异常");
}
}
}
ApiException ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ApiException(httpException, httpException.code());
ex.message = httpException.getMessage();
//如果是403,尝试让用户手动验证
if (httpException.code() == 403) {
EventBus.getDefault().post(new NeedCheckGoogleRecaptchaEvent());
}
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof JsonSerializer
|| e instanceof NotSerializableException
|| e instanceof ParseException) {
ex = new ApiException(e, Error.PARSE_ERROR);
ex.message = "数据解析错误";
return ex;
} else if (e instanceof ClassCastException) {
ex = new ApiException(e, Error.CAST_ERROR);
ex.message = "类型转换错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ApiException(e, Error.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ApiException(e, Error.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ApiException(e, Error.TIMEOUT_ERROR);
ex.message = "网络连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ApiException(e, Error.TIMEOUT_ERROR);
ex.message = "网络连接超时";
return ex;
} else if (e instanceof UnknownHostException) {
ex = new ApiException(e, Error.UNKNOWNHOST_ERROR);
ex.message = "无法解析该域名";
return ex;
} else if (e instanceof NullPointerException) {
if (!BuildConfig.DEBUG) {
//Bugsnag.notify(new Throwable("NullPointerException:" + MyApplication.getInstance().getDataManager().getPorn9VideoAddress() + ":::" + MyApplication.getInstance().getDataManager().getPorn9ForumAddress(), e), Severity.WARNING);
}
ex = new ApiException(e, Error.NULLPOINTER_EXCEPTION);
ex.message = "NullPointerException";
return ex;
} else if (e instanceof VideoException) {
ex = new ApiException(e, Error.PARSE_VIDEO_URL_ERROR);
ex.message = e.getMessage();
return ex;
} else if (e instanceof FavoriteException) {
ex = new ApiException(e, Error.FAVORITE_VIDEO_ERROR);
ex.message = e.getMessage();
return ex;
} else if (e instanceof DaoException) {
ex = new ApiException(e, Error.GREEN_DAO_ERROR);
ex.message = "数据库错误";
return ex;
} else if (e instanceof MessageException) {
ex = new ApiException(e, Error.COMMON_MESSAGE_ERROR);
ex.message = e.getMessage();
return ex;
} else {
ex = new ApiException(e, Error.UNKNOWN);
ex.message = "未知错误:" + e.getMessage();
return ex;
}
}
public static ApiException handleException(Throwable e) {
//使用RxCache之后返回的是包裹的CompositeException,一般包含2个异常,rxcache异常和原本的异常
Logger.t(TAG).d("开始解析错误------");
if (e instanceof CompositeException) {
CompositeException compositeException = (CompositeException) e;
for (Throwable throwable : compositeException.getExceptions()) {
if (!(throwable instanceof RxCacheException)) {
e = throwable;
Logger.t(TAG).d("其他异常:" + throwable.getMessage());
} else {
Logger.t(TAG).d("RxCache 异常");
}
}
}
ApiException ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ApiException(httpException, httpException.code());
ex.message = httpException.getMessage();
//如果是403,尝试让用户手动验证
if (httpException.code() == 403) {
EventBus.getDefault().post(new NeedCheckGoogleRecaptchaEvent());
}
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof JsonSerializer
|| e instanceof NotSerializableException
|| e instanceof ParseException) {
ex = new ApiException(e, Error.PARSE_ERROR);
ex.message = "数据解析错误";
return ex;
} else if (e instanceof ClassCastException) {
ex = new ApiException(e, Error.CAST_ERROR);
ex.message = "类型转换错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ApiException(e, Error.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ApiException(e, Error.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ApiException(e, Error.TIMEOUT_ERROR);
ex.message = "网络连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ApiException(e, Error.TIMEOUT_ERROR);
ex.message = "网络连接超时";
return ex;
} else if (e instanceof UnknownHostException) {
ex = new ApiException(e, Error.UNKNOWNHOST_ERROR);
ex.message = "无法解析该域名";
return ex;
} else if (e instanceof NullPointerException) {
if (!BuildConfig.DEBUG) {
//Bugsnag.notify(new Throwable("NullPointerException:" + MyApplication.getInstance().getDataManager().getPorn9VideoAddress() + ":::" + MyApplication.getInstance().getDataManager().getPorn9ForumAddress(), e), Severity.WARNING);
}
ex = new ApiException(e, Error.NULLPOINTER_EXCEPTION);
ex.message = "NullPointerException";
return ex;
} else if (e instanceof VideoException) {
ex = new ApiException(e, Error.PARSE_VIDEO_URL_ERROR);
ex.message = e.getMessage();
return ex;
} else if (e instanceof FavoriteException) {
ex = new ApiException(e, Error.FAVORITE_VIDEO_ERROR);
ex.message = e.getMessage();
return ex;
} else if (e instanceof DaoException) {
ex = new ApiException(e, Error.GREEN_DAO_ERROR);
ex.message = "数据库错误";
return ex;
} else if (e instanceof MessageException) {
ex = new ApiException(e, Error.COMMON_MESSAGE_ERROR);
ex.message = e.getMessage();
return ex;
} else {
ex = new ApiException(e, Error.UNKNOWN);
ex.message = "未知错误:" + e.getMessage();
return ex;
}
}
public static ResponseThrowable handleException(Throwable e) {
ResponseThrowable ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponseThrowable(e, ERROR.HTTP_ERROR);
switch (httpException.code()) {
case UNAUTHORIZED:
case FORBIDDEN:
case NOT_FOUND:
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
default:
ex.msg = "网络错误";
break;
}
} else if (e instanceof ServerException) {
ServerException resultException = (ServerException) e;
ex = new ResponseThrowable(resultException, resultException.result);
ex.msg = resultException.msg;
} else if (e instanceof ResponseThrowable) {
ex = (ResponseThrowable) e;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException) {
ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
ex.msg = "解析错误";
} else if (e instanceof ConnectException) {
ex = new ResponseThrowable(e, ERROR.NETWORD_ERROR);
ex.msg = "网络连接异常,请检查您的网络状态";
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
ex.msg = "证书验证失败";
} else if (e instanceof ConnectTimeoutException) {
ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
ex.msg = "网络连接超时,请检查您的网络状态,稍后重试";
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
ex.msg = "网络连接超时,请检查您的网络状态,稍后重试";
} else if (e instanceof UnknownHostException) {
ex = new ResponseThrowable(e, ERROR.UNKNOWN_HOST);
ex.msg = "网络连接异常,请检查您的网络状态";
} else {
ex = new ResponseThrowable(e, ERROR.UNKNOWN);
Throwable cause = e.getCause();
if(cause!=null){
String message = cause.getMessage();
if(TextUtils.isEmpty(message)){
message = cause.getLocalizedMessage();
}
ex.msg = message;
}else{
ex.msg = "未知错误";
}
}
return ex;
}
public static ResponseThrowable getResponseThrowable(Throwable e) {
ResponseThrowable ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponseThrowable();
switch (httpException.code()) {
case UNAUTHORIZED:
case FORBIDDEN:
ex.code = HTTP_ERROR;
ex.message = "请检查权限";
break;
case NOT_FOUND:
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
default:
ex.code = HTTP_ERROR;
ex.message = "网络错误";
break;
}
return ex;
} else if (e instanceof ServerException) {
ServerException resultException = (ServerException) e;
ex = new ResponseThrowable();
ex.code = resultException.code;
ex.message = resultException.message;
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException) {
ex = new ResponseThrowable();
ex.code = PARSE_ERROR;
ex.message = "解析错误";
return ex;
} else if (e instanceof Connection) {
ex = new ResponseThrowable();
ex.code = NETWORD_ERROR;
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ResponseThrowable();
ex.code = SSL_ERROR;
ex.message = "证书验证失败";
return ex;
} else {
ex = new ResponseThrowable();
ex.code = NETWORD_ERROR;
ex.message = "网络错误";
return ex;
}
}
public static ResponseThrowable handleException(Throwable e) {
ResponseThrowable ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponseThrowable(e, ERROR.HTTP_ERROR);
switch (httpException.code()) {
case UNAUTHORIZED:
ex.message = "操作未授权";
break;
case FORBIDDEN:
ex.message = "请求被拒绝";
break;
case NOT_FOUND:
ex.message = "资源不存在";
break;
case REQUEST_TIMEOUT:
ex.message = "服务器执行超时";
break;
case INTERNAL_SERVER_ERROR:
ex.message = "服务器内部错误";
break;
case SERVICE_UNAVAILABLE:
ex.message = "服务器不可用";
break;
default:
ex.message = "网络错误";
break;
}
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException || e instanceof MalformedJsonException) {
ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
ex.message = "解析错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ResponseThrowable(e, ERROR.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
} else {
ex = new ResponseThrowable(e, ERROR.UNKNOWN);
ex.message = "未知错误";
return ex;
}
}
public static ApiException handleException(Throwable e) {
ApiException ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ApiException(httpException, httpException.code());
/*switch (httpException.code()) {
case BADREQUEST:
case UNAUTHORIZED:
case FORBIDDEN:
case NOT_FOUND:
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
default:
ex.message = "网络错误,Code:"+httpException.code()+" ,err:"+httpException.getMessage();
break;
}*/
ex.message = httpException.getMessage();
return ex;
} else if (e instanceof ServerException) {
ServerException resultException = (ServerException) e;
ex = new ApiException(resultException, resultException.getErrCode());
ex.message = resultException.getMessage();
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof JsonSyntaxException
|| e instanceof JsonSerializer
|| e instanceof NotSerializableException
|| e instanceof ParseException) {
ex = new ApiException(e, ERROR.PARSE_ERROR);
ex.message = "解析错误";
return ex;
} else if (e instanceof ClassCastException) {
ex = new ApiException(e, ERROR.CAST_ERROR);
ex.message = "类型转换错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ApiException(e, ERROR.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ApiException(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
} else if (e instanceof UnknownHostException) {
ex = new ApiException(e, ERROR.UNKNOWNHOST_ERROR);
ex.message = "无法解析该域名";
return ex;
} else if (e instanceof NullPointerException) {
ex = new ApiException(e, ERROR.NULLPOINTER_EXCEPTION);
ex.message = "NullPointerException";
return ex;
} else {
ex = new ApiException(e, ERROR.UNKNOWN);
ex.message = "未知错误";
return ex;
}
}
public static ApiException handleException(Throwable e) {
ApiException ex;
if (e instanceof ServerException) { // HTTP错误
ServerException httpException = (ServerException) e;
ex = new ApiException(e, ErrorType.HTTP_ERROR);
switch (httpException.code) {
case FAIL:
ex.message = "userName or passWord is error!";
break;
case UNAUTHORIZED:
ex.message = "当前请求需要用户验证";
break;
case FORBIDDEN:
ex.message = "服务器已经理解请求,但是拒绝执行它";
break;
case NOT_FOUND:
ex.message = "服务器异常,请稍后再试";
break;
case REQUEST_TIMEOUT:
ex.message = "请求超时";
break;
case GATEWAY_TIMEOUT:
ex.message = "作为网关或者代理工作的服务器尝试执行请求时,未能及时从上游服务器(URI标识出的服务器,例如HTTP、FTP、LDAP" +
")或者辅助服务器(例如DNS)收到响应";
break;
case INTERNAL_SERVER_ERROR:
ex.message = "服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理";
break;
case BAD_GATEWAY:
ex.message = "作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应";
break;
case SERVICE_UNAVAILABLE:
ex.message = "由于临时的服务器维护或者过载,服务器当前无法处理请求";
break;
default:
ex.message = "网络错误"; // 其它均视为网络错误
break;
}
return ex;
} else if (e instanceof ServerException) { // 服务器返回的错误
ServerException resultException = (ServerException) e;
ex = new ApiException(resultException, resultException.code);
ex.message = resultException.message;
return ex;
} else if (e instanceof JSONException
|| e instanceof ParseException) {
ex = new ApiException(e, ErrorType.PARSE_ERROR);
ex.message = "解析错误"; // 均视为解析错误
return ex;
} else if (e instanceof ConnectException || e instanceof SocketTimeoutException || e
instanceof ConnectTimeoutException) {
ex = new ApiException(e, ErrorType.NETWORK_ERROR);
ex.message = "连接失败"; // 均视为网络错误
return ex;
} else if (e instanceof HttpException) {
if ("HTTP 404 Not Found".equals(e.getMessage())) {
ex = new ApiException(e, ErrorType.NETWORK_ERROR);
ex.message = "没有连接服务器";
} else {
ex = new ApiException(e, ErrorType.NETWORK_ERROR);
ex.message = "其他连接服务器错误";
}
return ex;
} else {
ex = new ApiException(e, ErrorType.UNKONW);
ex.message = "未知错误"; // 未知错误
return ex;
}
}
public static ResponeThrowable handleException(Throwable e) {
ResponeThrowable ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponeThrowable(e, ERROR.HTTP_ERROR);
switch (httpException.code()) {
case UNAUTHORIZED:
case FORBIDDEN:
case NOT_FOUND:
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
default:
ex.message = "网络错误";
break;
}
return ex;
} else if (e instanceof ServerException) {
ServerException resultException = (ServerException) e;
ex = new ResponeThrowable(resultException, resultException.code);
ex.message = resultException.message;
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException) {
ex = new ResponeThrowable(e, ERROR.PARSE_ERROR);
ex.message = "解析错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ResponeThrowable(e, ERROR.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException){
ex = new ResponeThrowable(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ResponeThrowable(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
}
else {
ex = new ResponeThrowable(e, ERROR.UNKNOWN);
ex.message = "未知错误";
return ex;
}
}
public static ResponeThrowable handleException(Throwable e) {
ResponeThrowable ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponeThrowable(e, ERROR.HTTP_ERROR);
switch (httpException.code()) {
case UNAUTHORIZED:
case FORBIDDEN:
case NOT_FOUND:
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
default:
ex.message = GankApp.context().getString(R.string.network_error);
break;
}
return ex;
} else if (e instanceof ServerException) {
ServerException resultException = (ServerException) e;
ex = new ResponeThrowable(resultException, resultException.code);
ex.message = resultException.message;
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException) {
ex = new ResponeThrowable(e, ERROR.PARSE_ERROR);
ex.message = GankApp.context().getString(R.string.network_error_parse);
return ex;
} else if (e instanceof ConnectException) {
ex = new ResponeThrowable(e, ERROR.NETWORD_ERROR);
ex.message = GankApp.context().getString(R.string.network_error_connect);
return ex;
} else if (e instanceof SSLHandshakeException) {
ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
ex.message = GankApp.context().getString(R.string.network_error_ssl);
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ResponeThrowable(e, ERROR.TIMEOUT_ERROR);
ex.message = GankApp.context().getString(R.string.network_error_timeout);
return ex;
} else if (e instanceof SocketTimeoutException) {
ex = new ResponeThrowable(e, ERROR.TIMEOUT_ERROR);
ex.message = GankApp.context().getString(R.string.network_error_timeout);
return ex;
} else {
ex = new ResponeThrowable(e, ERROR.UNKNOWN);
ex.message = GankApp.context().getString(R.string.network_error_unknown);
return ex;
}
}
public static ApiException handleException(Throwable e) {
ApiException ex;
if (e instanceof ServerException) { //HTTP错误
ServerException httpException = (ServerException) e;
ex = new ApiException(e, ErrorType.HTTP_ERROR);
switch (httpException.code) {
case FAIL:
ex.message = "userName or passWord is error!";
break;
case UNAUTHORIZED:
ex.message = "当前请求需要用户验证";
break;
case FORBIDDEN:
ex.message = "服务器已经理解请求,但是拒绝执行它";
break;
case NOT_FOUND:
ex.message = "服务器异常,请稍后再试";
break;
case REQUEST_TIMEOUT:
ex.message = "请求超时";
break;
case GATEWAY_TIMEOUT:
ex.message = "作为网关或者代理工作的服务器尝试执行请求时,未能及时从上游服务器(URI标识出的服务器,例如HTTP、FTP、LDAP" +
")或者辅助服务器(例如DNS)收到响应";
break;
case INTERNAL_SERVER_ERROR:
ex.message = "服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理";
break;
case BAD_GATEWAY:
ex.message = "作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应";
break;
case SERVICE_UNAVAILABLE:
ex.message = "由于临时的服务器维护或者过载,服务器当前无法处理请求";
break;
default:
ex.message = "网络错误"; //其它均视为网络错误
break;
}
return ex;
} else if (e instanceof ServerException) { //服务器返回的错误
ServerException resultException = (ServerException) e;
ex = new ApiException(resultException, resultException.code);
ex.message = resultException.message;
return ex;
} else if (e instanceof JSONException
|| e instanceof ParseException) {
ex = new ApiException(e, ErrorType.PARSE_ERROR);
ex.message = "解析错误"; //均视为解析错误
return ex;
} else if (e instanceof ConnectException || e instanceof SocketTimeoutException || e
instanceof ConnectTimeoutException) {
ex = new ApiException(e, ErrorType.NETWORK_ERROR);
ex.message = "连接失败"; //均视为网络错误
return ex;
} else if (e instanceof HttpException) {
if ("HTTP 404 Not Found".equals(e.getMessage())) {
ex = new ApiException(e, ErrorType.NETWORK_ERROR);
ex.message = "没有连接服务器";
} else {
ex = new ApiException(e, ErrorType.NETWORK_ERROR);
ex.message = "其他连接服务器错误";
}
return ex;
} else {
ex = new ApiException(e, ErrorType.UNKONW);
ex.message = "未知错误"; //未知错误
return ex;
}
}
public static ApiException handleException(Throwable e){
ApiException ex;
if (e instanceof HttpException){ //HTTP错误
HttpException httpException = (HttpException) e;
ex = new ApiException(e, ErrorType.HTTP_ERROR);
switch(httpException.code()){
case UNAUTHORIZED:
ex.message = "当前请求需要用户验证";
break;
case FORBIDDEN:
ex.message = "服务器已经理解请求,但是拒绝执行它";
break;
case NOT_FOUND:
ex.message = "服务器异常,请稍后再试";
break;
case REQUEST_TIMEOUT:
ex.message = "请求超时";
break;
case GATEWAY_TIMEOUT:
ex.message = "作为网关或者代理工作的服务器尝试执行请求时,未能及时从上游服务器(URI标识出的服务器,例如HTTP、FTP、LDAP)或者辅助服务器(例如DNS)收到响应";
break;
case INTERNAL_SERVER_ERROR:
ex.message = "服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理";
break;
case BAD_GATEWAY:
ex.message = "作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应";
break;
case SERVICE_UNAVAILABLE:
ex.message = "由于临时的服务器维护或者过载,服务器当前无法处理请求";
break;
default:
ex.message = "网络错误"; //其它均视为网络错误
break;
}
return ex;
} else if (e instanceof ServerException){ //服务器返回的错误
ServerException resultException = (ServerException) e;
ex = new ApiException(resultException, resultException.code);
ex.message = resultException.message;
return ex;
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException){
ex = new ApiException(e, ErrorType.PARSE_ERROR);
ex.message = "解析错误"; //均视为解析错误
return ex;
}else if(e instanceof ConnectException || e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException){
ex = new ApiException(e, ErrorType.NETWORD_ERROR);
ex.message = "连接失败"; //均视为网络错误
return ex;
}
else {
ex = new ApiException(e, ErrorType.UNKNOWN);
ex.message = "未知错误"; //未知错误
return ex;
}
}
@Override
protected Boolean doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(10000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int status = conn.getResponseCode();
if (status == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
JSONObject movieData = new JSONObject(sb.toString());
totalPages = movieData.getInt("total_pages");
JSONArray movieArray = movieData.getJSONArray("results");
// is added checks if we are still on the same view, if we don't do this check the program will crash
if (isAdded()) {
for (int i = 0; i < movieArray.length(); i++) {
JSONObject object = movieArray.getJSONObject(i);
MovieModel movie = new MovieModel();
movie.setId(object.getInt("id"));
movie.setTitle(object.getString("title"));
if (!object.getString("release_date").equals("null") && !object.getString("release_date").isEmpty())
movie.setReleaseDate(object.getString("release_date"));
if (!object.getString("poster_path").equals("null") && !object.getString("poster_path").isEmpty())
movie.setPosterPath(MovieDB.imageUrl + getResources().getString(R.string.imageSize) + object.getString("poster_path"));
moviesList.add(movie);
}
return true;
}
}
} catch (ParseException | IOException | JSONException e) {
if (conn != null)
conn.disconnect();
} finally {
if (conn != null)
conn.disconnect();
}
return false;
}
@Override
protected Boolean doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(10000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int status = conn.getResponseCode();
if (status == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
JSONObject searchData = new JSONObject(sb.toString());
totalPages = searchData.getInt("total_pages");
JSONArray searchResultsArray = searchData.getJSONArray("results");
for (int i = 0; i < searchResultsArray.length(); i++) {
JSONObject object = searchResultsArray.getJSONObject(i);
SearchModel movie = new SearchModel();
if (object.has("id") && object.getInt("id") != 0)
movie.setId(object.getInt("id"));
if (object.has("title"))
movie.setTitle(object.getString("title"));
if (object.has("name")) {
if (object.has("media_type") && object.getString("media_type").equals("tv"))
movie.setTitle(object.getString("name"));
else
movie.setTitle(object.getString("name"));
}
if (object.has("release_date") && !object.getString("release_date").equals("null") && !object.getString("release_date").isEmpty())
movie.setReleaseDate(object.getString("release_date"));
if (object.has("first_air_date") && !object.getString("first_air_date").equals("null") && !object.getString("first_air_date").isEmpty())
movie.setReleaseDate(object.getString("first_air_date"));
// is added checks if we are still on the same view, if we don't do this check the program will crash
if (isAdded()) {
if (object.has("poster_path") && !object.getString("poster_path").equals("null") && !object.getString("poster_path").isEmpty())
movie.setPosterPath(MovieDB.imageUrl + getResources().getString(R.string.imageSize) + object.getString("poster_path"));
}
if (isAdded()) {
if (object.has("profile_path") && !object.getString("profile_path").equals("null") && !object.getString("profile_path").isEmpty())
movie.setPosterPath(MovieDB.imageUrl + getResources().getString(R.string.imageSize) + object.getString("profile_path"));
}
if (object.has("media_type") && !object.getString("media_type").isEmpty())
movie.setMediaType(object.getString("media_type"));
searchList.add(movie);
}
return true;
}
} catch (ParseException | IOException | JSONException e) {
if (conn != null)
conn.disconnect();
} finally {
if (conn != null)
conn.disconnect();
}
return false;
}
@Override
protected Boolean doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(10000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int status = conn.getResponseCode();
if (status == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
JSONObject movieData = new JSONObject(sb.toString());
totalPages = movieData.getInt("total_pages");
JSONArray movieArray = movieData.getJSONArray("results");
// is added checks if we are still on the same view, if we don't do this check the program will crash
if (isAdded()) {
for (int i = 0; i < movieArray.length(); i++) {
JSONObject object = movieArray.getJSONObject(i);
MovieModel movie = new MovieModel();
movie.setId(object.getInt("id"));
movie.setTitle(object.getString("name"));
if (!object.getString("first_air_date").equals("null") && !object.getString("first_air_date").isEmpty())
movie.setReleaseDate(object.getString("first_air_date"));
if (!object.getString("poster_path").equals("null") && !object.getString("poster_path").isEmpty())
movie.setPosterPath(MovieDB.imageUrl + getResources().getString(R.string.imageSize) + object.getString("poster_path"));
tvList.add(movie);
}
return true;
}
}
} catch (ParseException | IOException | JSONException e) {
if (conn != null)
conn.disconnect();
} finally {
if (conn != null)
conn.disconnect();
}
return false;
}