|
@@ -25,31 +25,29 @@ import java.util.Map;
|
|
|
|
|
|
public class Http {
|
|
|
//============样式属性===================
|
|
|
- public final static int LOAD_HIDE = 0x0;
|
|
|
- public final static int LOAD_SHOW = 0x1;
|
|
|
- public final static int LOAD_FIXED_SHOW = 0x12;
|
|
|
- public final static int ERROR_TOAST = 0x2;
|
|
|
- public final static int ERROR_DIALOG = 0x3;
|
|
|
- public final static int ERROR_HIDE = 0x4;
|
|
|
- public final static int ONLY_RETRY = 0;
|
|
|
- public final static int CANCEL_AND_RETRY = 1;
|
|
|
- public final static int FINISH_AND_RETRY = 2;
|
|
|
- private static List<Callback.Cancelable> cancelableList = getCancelList();
|
|
|
- private final Map<String, Object> params;
|
|
|
- private String url;
|
|
|
- private int timeout = 15 * 1000;
|
|
|
- private LoadingDialog dialog;
|
|
|
- private boolean isBindLife = true;
|
|
|
- private Callback.Cancelable cancelable;
|
|
|
- private Map<String, String> headMap;
|
|
|
+ public final static int LOAD_HIDE = 0x0;
|
|
|
+ public final static int LOAD_SHOW = 0x1;
|
|
|
+ public final static int LOAD_FIXED_SHOW = 0x12;
|
|
|
+ public final static int ERROR_TOAST = 0x2;
|
|
|
+ public final static int ERROR_DIALOG = 0x3;
|
|
|
+ public final static int ERROR_HIDE = 0x4;
|
|
|
+ public final static int ONLY_RETRY = 0;
|
|
|
+ public final static int CANCEL_AND_RETRY = 1;
|
|
|
+ public final static int FINISH_AND_RETRY = 2;
|
|
|
+ private final Map<String, Object> params; private static List<Callback.Cancelable> cancelableList = getCancelList();
|
|
|
+ private String url;
|
|
|
+ private int timeout = 15 * 1000;
|
|
|
+ private LoadingDialog dialog;
|
|
|
+ private boolean isBindLife = true;
|
|
|
+ private Callback.Cancelable cancelable;
|
|
|
+ private Map<String, String> headMap;
|
|
|
//样式配置
|
|
|
- private int loadStyle = LOAD_SHOW; //默认显示网络加载框
|
|
|
- private int errorStyle = ERROR_TOAST; //失败后展示的样式
|
|
|
- private int retryStyle = CANCEL_AND_RETRY;//超时弹窗样式
|
|
|
- private int retryCount = 0;//超时次数 默认不重试
|
|
|
+ private int loadStyle = LOAD_SHOW; //默认显示网络加载框
|
|
|
+ private int errorStyle = ERROR_TOAST; //失败后展示的样式
|
|
|
+ private int retryStyle = CANCEL_AND_RETRY;//超时弹窗样式
|
|
|
+ private int retryCount = 0;//超时次数 默认不重试
|
|
|
//======================================
|
|
|
- private boolean headerOther;
|
|
|
-
|
|
|
+ private boolean headerOther;
|
|
|
public Http() {
|
|
|
params = new ArrayMap<>();
|
|
|
}
|
|
@@ -91,7 +89,6 @@ public class Http {
|
|
|
return JSON.parseObject(json, clz);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public Http setUrlPath(String url, String path) {
|
|
|
this.url = url + path;
|
|
|
return this;
|
|
@@ -177,8 +174,7 @@ public class Http {
|
|
|
if (loadStyle == LOAD_SHOW) {
|
|
|
dialog.setOnDismissListener(dialogInterface -> cancelable.cancel());
|
|
|
dialog.show();
|
|
|
- }
|
|
|
- else if (loadStyle == LOAD_FIXED_SHOW) {
|
|
|
+ } else if (loadStyle == LOAD_FIXED_SHOW) {
|
|
|
dialog.setCancelable(false);
|
|
|
dialog.setCanceledOnTouchOutside(false);
|
|
|
dialog.show();
|
|
@@ -186,8 +182,7 @@ public class Http {
|
|
|
}
|
|
|
|
|
|
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
ThreadUtils.runOnUiThread(() -> {
|
|
|
Activity activity = ActivityUtils.getTopActivity();
|
|
|
if (!ActivityUtils.isActivityAlive(activity)) {
|
|
@@ -202,8 +197,7 @@ public class Http {
|
|
|
if (loadStyle == LOAD_SHOW) {
|
|
|
dialog.setOnDismissListener(dialogInterface -> cancelable.cancel());
|
|
|
dialog.show();
|
|
|
- }
|
|
|
- else if (loadStyle == LOAD_FIXED_SHOW) {
|
|
|
+ } else if (loadStyle == LOAD_FIXED_SHOW) {
|
|
|
dialog.setCancelable(false);
|
|
|
dialog.setCanceledOnTouchOutside(false);
|
|
|
dialog.show();
|
|
@@ -212,8 +206,7 @@ public class Http {
|
|
|
|
|
|
});
|
|
|
}
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
LogUtils.e(e);
|
|
|
}
|
|
|
}
|
|
@@ -222,8 +215,7 @@ public class Http {
|
|
|
if (dialog != null) {
|
|
|
if (ThreadUtils.isMainThread()) {
|
|
|
dialog.dismiss();
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
ThreadUtils.runOnUiThread(() -> {
|
|
|
dialog.dismiss();
|
|
|
});
|
|
@@ -261,8 +253,8 @@ public class Http {
|
|
|
public void onSuccess(BaseBean result) {
|
|
|
if (result.getSuccess()) {
|
|
|
Type mySuperclass = callBack.getClass().getGenericSuperclass();
|
|
|
- Type tType = ((ParameterizedType) mySuperclass).getActualTypeArguments()[0];
|
|
|
- T model;
|
|
|
+ Type tType = ((ParameterizedType) mySuperclass).getActualTypeArguments()[0];
|
|
|
+ T model;
|
|
|
|
|
|
if (result.getData() == null && tType.equals(BaseBean.class)) {
|
|
|
callBack.onNext((T) result);
|
|
@@ -271,13 +263,11 @@ public class Http {
|
|
|
|
|
|
try {
|
|
|
model = Http.parser(result.getData(), tType);
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
model = GsonUtils.fromJson(result.getData(), tType);
|
|
|
}
|
|
|
callBack.onNext(model);
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
showError(result.getMsg());
|
|
|
callBack.onFail(result);
|
|
|
}
|
|
@@ -325,8 +315,8 @@ public class Http {
|
|
|
public void onSuccess(BaseBean result) {
|
|
|
if (result.getSuccess()) {
|
|
|
Type mySuperclass = callBack.getClass().getGenericSuperclass();
|
|
|
- Type tType = ((ParameterizedType) mySuperclass).getActualTypeArguments()[0];
|
|
|
- T model;
|
|
|
+ Type tType = ((ParameterizedType) mySuperclass).getActualTypeArguments()[0];
|
|
|
+ T model;
|
|
|
|
|
|
if (result.getData() == null && tType.equals(BaseBean.class)) {
|
|
|
callBack.onNext((T) result);
|
|
@@ -335,13 +325,11 @@ public class Http {
|
|
|
|
|
|
try {
|
|
|
model = Http.parser(result.getData(), tType);
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
model = GsonUtils.fromJson(result.getData(), tType);
|
|
|
}
|
|
|
callBack.onNext(model);
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
showError(result.getMsg());
|
|
|
callBack.onFail(result);
|
|
|
}
|
|
@@ -367,7 +355,6 @@ public class Http {
|
|
|
if (isBindLife) cancelableList.add(cancelable);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public void postTest() {
|
|
|
showLoading();
|
|
|
RequestParams request = headerOther ? setHeaderOtherMap(new RequestParams(url)) : setHeaderMap(new RequestParams(url));
|
|
@@ -410,4 +397,6 @@ public class Http {
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
}
|