Skip to content

Commit

Permalink
Fix Dialog to display the problem
Browse files Browse the repository at this point in the history
Fix Dialog to display the problem
  • Loading branch information
liqi committed Oct 20, 2017
1 parent 7b60c13 commit 7b9dbf4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 57 deletions.
110 changes: 57 additions & 53 deletions nohttputils/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
class RxNoHttp {
private static RxNoHttp rxNoHttp;
private Dialog dialog = null;

private RxNoHttp() {

Expand All @@ -45,88 +44,91 @@ synchronized static RxNoHttp getRxNoHttp() {
* 通过nohttp去请求
*
* @param mDialogGetListener dialog获取接口
* @param responseInterfa 请求成功或者失败回调对象
* @param responseInterfa 请求成功或者失败回调对象
*/
<T> void request(final ProtocolRequest<?,T> request, DialogGetListener mDialogGetListener, final OnIsRequestListener<T> responseInterfa) {
if (null != mDialogGetListener)
dialog = mDialogGetListener.getDialog();

if (null != dialog)
dialog.show();
<T> void request(final ProtocolRequest<?, T> request, DialogGetListener mDialogGetListener, final OnIsRequestListener<T> 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<Response<T>>() {
@Override
public void call(Subscriber<? super Response<T>> subscriberOut) {
// 最关键的就是用NoHttp的同步请求请求到response了,其它的都是rxjava做的,跟nohttp无关的。
Response<T> 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<Response<T>>() {
@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);
}
}

@Override
public void onNext(Response<T> 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());
}
Expand All @@ -139,8 +141,10 @@ public void onNext(Response<T> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ private <T> void runRequest(final RxRequestModel<T> 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)
Expand All @@ -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不存在");
}
}

// 提示异常信息。
Expand Down Expand Up @@ -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<T> onIsRequestListener = baseRxRequestModel.getOnIsRequestListener();
Expand Down

0 comments on commit 7b9dbf4

Please sign in to comment.