diff --git a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java index 5ef3210..c9e3d73 100644 --- a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java +++ b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java @@ -31,7 +31,6 @@ */ class RxNoHttp { private static RxNoHttp rxNoHttp; - private Dialog dialog = null; private RxNoHttp() { @@ -45,81 +44,75 @@ synchronized static RxNoHttp getRxNoHttp() { * 通过nohttp去请求 * * @param mDialogGetListener dialog获取接口 - * @param responseInterfa 请求成功或者失败回调对象 + * @param responseInterfa 请求成功或者失败回调对象 */ - void request(final ProtocolRequest request, DialogGetListener mDialogGetListener, final OnIsRequestListener responseInterfa) { - if (null != mDialogGetListener) - dialog = mDialogGetListener.getDialog(); - - if (null != dialog) - dialog.show(); + void request(final ProtocolRequest request, DialogGetListener mDialogGetListener, final OnIsRequestListener responseInterfa) { + final Dialog dialog = null == mDialogGetListener ? null : mDialogGetListener.getDialog(); + if (null != dialog&&!dialog.isShowing()) { + try { + dialog.show(); + } catch (Exception e) { + Logger.e("Dialog-显示异常:由于Dialog依赖的Context不是栈顶。"); + } + } Observable.create(new Observable.OnSubscribe>() { @Override public void call(Subscriber> subscriberOut) { // 最关键的就是用NoHttp的同步请求请求到response了,其它的都是rxjava做的,跟nohttp无关的。 Response response = NoHttp.startRequestSync(request); -// byte[] oo=new byte[12]; -// try { -// response.request().parseResponse(response.getHeaders(),oo); -// } catch (Exception e) { -// e.printStackTrace(); -// } - - if (response.isSucceed()||response.isFromCache()) { + if (response.isSucceed() || response.isFromCache()) { subscriberOut.onNext(response); - } - else { + } else { subscriberOut.onError(response.getException()); } - subscriberOut.onCompleted(); + } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber>() { @Override public void onCompleted() { - if (null != dialog) { - if (dialog.isShowing()) { - dialog.dismiss(); - } - dialog = null; - } + } @Override public void onError(Throwable e) { // 关闭dialog. - if (null != dialog) { - if (dialog.isShowing()) { + if (null != dialog && dialog.isShowing()) { + try { dialog.dismiss(); + } catch (Exception e1) { + Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); } - // 提示异常信息。 - if (e instanceof NetworkError) {// 网络不好 - show(R.string.error_please_check_network); - } else if (e instanceof TimeoutError) {// 请求超时 - show(R.string.error_timeout); - } else if (e instanceof UnKnownHostError) {// 找不到服务器 - show(R.string.error_not_found_server); - } else if (e instanceof URLError) {// URL是错的 - show(R.string.error_url_error); - } else if (e instanceof NotFoundCacheError) { - // 这个异常只会在仅仅查找缓存时没有找到缓存时返回 - show(R.string.error_not_found_cache); - } else if (e instanceof ProtocolException) { - show(R.string.error_system_unsupport_method); - } else { - Logger.e("NoHttpUtils捕获异常:"+e.toString()); - StackTraceElement[] stackTrace = e.getStackTrace(); - if (null!=stackTrace) { - for (StackTraceElement traceElement : stackTrace) { - Logger.e("NoHttpUtils捕获异常:"+traceElement.toString()); - } + } + + // 提示异常信息。 + if (e instanceof NetworkError) {// 网络不好 + show(dialog, R.string.error_please_check_network); + } else if (e instanceof TimeoutError) {// 请求超时 + show(dialog, R.string.error_timeout); + } else if (e instanceof UnKnownHostError) {// 找不到服务器 + show(dialog, R.string.error_not_found_server); + } else if (e instanceof URLError) {// URL是错的 + show(dialog, R.string.error_url_error); + } else if (e instanceof NotFoundCacheError) { + // 这个异常只会在仅仅查找缓存时没有找到缓存时返回 + show(dialog, R.string.error_not_found_cache); + } else if (e instanceof ProtocolException) { + show(dialog, R.string.error_system_unsupport_method); + } else { + Logger.e("NoHttpUtils捕获异常:" + e.toString()); + StackTraceElement[] stackTrace = e.getStackTrace(); + if (null != stackTrace) { + for (StackTraceElement traceElement : stackTrace) { + Logger.e("NoHttpUtils捕获异常:" + traceElement.toString()); } - show(R.string.error_unknow); } - dialog = null; + show(dialog, R.string.error_unknow); } + + if (null != responseInterfa) { responseInterfa.onError(e); } @@ -127,6 +120,15 @@ public void onError(Throwable e) { @Override public void onNext(Response tResponse) { + // 关闭dialog. + if (null != dialog && dialog.isShowing()) { + try { + dialog.dismiss(); + } catch (Exception e1) { + Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); + } + } + if (null != responseInterfa) { responseInterfa.onNext(tResponse.get()); } @@ -139,8 +141,10 @@ public void onNext(Response tResponse) { * * @param stringId 提示内容资源ID */ - private void show(int stringId) { - Context context = dialog.getContext(); - Toast.makeText(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT).show(); + private void show(Dialog dialog, int stringId) { + if (null != dialog) { + Context context = dialog.getContext(); + Toast.makeText(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT).show(); + } } } diff --git a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java index 51f7e0a..4fcc4a3 100644 --- a/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java +++ b/nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java @@ -129,8 +129,12 @@ private void runRequest(final RxRequestModel baseRxRequestModel) { if (null != baseRxRequestModel) { Dialog dialog = getDialog(baseRxRequestModel); - if (null != dialog) { - dialog.show(); + if (null != dialog&&!dialog.isShowing()) { + try { + dialog.show(); + } catch (Exception e) { + Logger.e("Dialog-显示异常:由于Dialog依赖的Context不是栈顶。"); + } } Observable.create(baseRxRequestModel) @@ -145,7 +149,11 @@ public void onCompleted() { public void onError(Throwable e) { Dialog dialog = getDialog(baseRxRequestModel); if (null != dialog && dialog.isShowing()) { - dialog.dismiss(); + try { + dialog.dismiss(); + } catch (Exception e1) { + Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); + } } // 提示异常信息。 @@ -190,7 +198,11 @@ public void onError(Throwable e) { public void onNext(T t) { Dialog dialog = getDialog(baseRxRequestModel); if (null != dialog && dialog.isShowing()) { - dialog.dismiss(); + try { + dialog.dismiss(); + } catch (Exception e) { + Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); + } } OnIsRequestListener onIsRequestListener = baseRxRequestModel.getOnIsRequestListener();