| | |
| | | |
| | | import cn.sinata.xldutils.activitys.BaseActivity; |
| | | import cn.sinata.xldutils.fragment.BaseFragment; |
| | | import cn.sinata.xldutils.net.utils.Error; |
| | | import cn.sinata.xldutils.net.utils.ResultException; |
| | | import cn.sinata.xldutils.utils.StringUtils; |
| | | import cn.sinata.xldutils.utils.Toast; |
| | |
| | | WeakReference<BaseActivity> activities; |
| | | WeakReference<BaseFragment> fragments; |
| | | Context context; |
| | | |
| | | public static final String REQUEST_ERROR = "请求出错!"; |
| | | public static final String PARSER_ERROR = "数据解析出错!"; |
| | | public static final String SERVER_ERROR = "服务器异常,请稍后重试!"; |
| | | public static final String NET_ERROR = "未连接到服务器!"; |
| | | public static final String NET_TIMEOUT = "链接超时,请稍后再试!"; |
| | | |
| | | |
| | | public FaceObserver(BaseActivity activity) { |
| | | activities = new WeakReference<>(activity); |
| | |
| | | LogUtils.e("responseString", "**" + responseString); |
| | | if (StringUtils.isEmpty(responseString)) { //判空 |
| | | int code = -1; |
| | | String msg = PARSER_ERROR; |
| | | onError(code, msg,responseString); |
| | | String msg = getShowErrorMsg(Error.PARSER_ERROR); |
| | | onError(code, msg, responseString); |
| | | return; |
| | | } |
| | | // success(responseString); |
| | |
| | | Utils.systemErr(e); |
| | | Log.e("responseString", "responseString get " + e.toString()); |
| | | int code = -1; |
| | | String msg = REQUEST_ERROR; |
| | | String msg = getShowErrorMsg(Error.REQUEST_ERROR); |
| | | if (e instanceof JsonSyntaxException || |
| | | e instanceof NumberFormatException || |
| | | e instanceof IllegalStateException) { |
| | | msg = PARSER_ERROR; |
| | | msg = getShowErrorMsg(Error.PARSER_ERROR); |
| | | } else if (e instanceof HttpException) { |
| | | msg = SERVER_ERROR; |
| | | msg = getShowErrorMsg(Error.SERVER_ERROR); |
| | | } else if (e instanceof ConnectException) { |
| | | msg = NET_ERROR; |
| | | msg = getShowErrorMsg(Error.NET_ERROR); |
| | | } else if (e instanceof SocketTimeoutException) { |
| | | msg = NET_TIMEOUT; |
| | | msg = getShowErrorMsg(Error.NET_TIMEOUT); |
| | | } else if (e instanceof ResultException) { |
| | | code = ((ResultException) e).getCode(); |
| | | msg = e.getMessage(); |
| | | } |
| | | try { |
| | | onError(code, msg,""); |
| | | onError(code, msg, ""); |
| | | } catch (Exception es) { |
| | | es.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | protected void onError(int code, String msg,String resposeString) { |
| | | protected void onError(int code, String msg, String resposeString) { |
| | | if (shouldShowErrorToast()) { |
| | | showToast(msg); |
| | | } |
| | |
| | | public void dispose() { |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 获取异常显示信息 |
| | | */ |
| | | public String getShowErrorMsg(Error error) { |
| | | int typeByLanguage = HttpManager.getTypeByLanguage(); |
| | | return switch (typeByLanguage) { |
| | | case 3 -> error.getErrorFr(); |
| | | case 1 -> error.getErrorCh(); |
| | | default -> error.getErrorEn(); |
| | | }; |
| | | } |
| | | } |