From f8acbd27acbfa2d14ffcae3d987f3d7768e7a15e Mon Sep 17 00:00:00 2001 From: Ztiany Date: Wed, 20 Nov 2019 18:00:59 +0800 Subject: [PATCH] optimize code --- .../base/utils/android/DevicesUtils.java | 16 +++++---- .../base/utils/android/views/SizeEx.kt | 2 ++ .../net/errorhandler/ErrorMessageFactory.java | 34 +++++++++++-------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lib_base/src/main/java/com/android/base/utils/android/DevicesUtils.java b/lib_base/src/main/java/com/android/base/utils/android/DevicesUtils.java index 98a6e99..77ab34a 100644 --- a/lib_base/src/main/java/com/android/base/utils/android/DevicesUtils.java +++ b/lib_base/src/main/java/com/android/base/utils/android/DevicesUtils.java @@ -64,7 +64,7 @@ public class DevicesUtils { serial = Build.class.getField("SERIAL").get(null).toString(); } } catch (Exception e) { - Timber.e(e,"getDeviceId"); + Timber.w("getDeviceId error: " + e.getMessage()); } if (serial == null) { //serial 需要一个初始化 @@ -74,9 +74,8 @@ public class DevicesUtils { return new UUID(M_SZ_DEV_ID_SHORT.hashCode(), serial.hashCode()).toString(); } - @SuppressLint("ObsoleteSdkInt") - public static String printSystemInfo() { + public static void printSystemInfo() { Date date = new Date(System.currentTimeMillis()); @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = dateFormat.format(date); @@ -117,11 +116,14 @@ public class DevicesUtils { } sb.append("\n_______ GINGERBREAD-9 _______"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { - sb.append("\nSERIAL :").append(Build.SERIAL); + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + sb.append("\nSERIAL :").append(Build.SERIAL); + } + } catch (Exception ignore) { } + Log.i("DEVICES", sb.toString()); - return sb.toString(); } -} +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/utils/android/views/SizeEx.kt b/lib_base/src/main/java/com/android/base/utils/android/views/SizeEx.kt index 7c47d34..fd013d6 100644 --- a/lib_base/src/main/java/com/android/base/utils/android/views/SizeEx.kt +++ b/lib_base/src/main/java/com/android/base/utils/android/views/SizeEx.kt @@ -1,3 +1,5 @@ +@file:JvmName("Sizes") + package com.android.base.utils.android.views import android.util.TypedValue diff --git a/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java b/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java index 87f214e..0ec3ca5 100644 --- a/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java +++ b/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java @@ -9,7 +9,9 @@ import com.android.sdk.net.exception.ServerErrorException; import com.android.sdk.net.provider.ErrorMessage; import java.io.IOException; +import java.util.List; +import io.reactivex.exceptions.CompositeException; import retrofit2.HttpException; import timber.log.Timber; @@ -24,42 +26,46 @@ public class ErrorMessageFactory { ErrorMessage mErrorMessage = NetContext.get().netProvider().errorMessage(); Timber.d("createMessage with:%s", exception.toString()); + //handle rx CompositeException + if (exception instanceof CompositeException) { + List exceptions = ((CompositeException) exception).getExceptions(); + if (exceptions != null && !exceptions.isEmpty()) { + exception = exceptions.get(0); + } + } + CharSequence message = null; - //SocketTimeoutException android NetworkErrorException extends IOException + //SocketTimeoutException, NetworkErrorException extends IOException //1:网络连接错误处理 if (exception instanceof IOException || exception instanceof NetworkErrorException) { message = mErrorMessage.netErrorMessage(exception); } + //2:服务器错误处理 else if (exception instanceof ServerErrorException) { - int errorType = ((ServerErrorException) exception).getErrorType(); if (errorType == ServerErrorException.SERVER_DATA_ERROR) { message = mErrorMessage.serverDataErrorMessage(exception); } else if (errorType == ServerErrorException.UNKNOW_ERROR) { message = mErrorMessage.serverErrorMessage(exception); } - } + //3:响应码非200处理 else if (exception instanceof HttpException) { - int code = ((HttpException) exception).code(); if (code >= 500/*http 500 表示服务器错误*/) { message = mErrorMessage.serverErrorMessage(exception); } else if (code >= 400/*http 400 表示客户端请求出错*/) { message = mErrorMessage.clientRequestErrorMessage(exception); } + } - } else { - //4:api 错误处理 - if (exception instanceof ApiErrorException) { - message = exception.getMessage(); - if (TextUtils.isEmpty(message)) { - message = mErrorMessage.apiErrorMessage((ApiErrorException) exception); - } - } else { - throw new RuntimeException(exception); + //4:Api Error + else if (exception instanceof ApiErrorException) { + message = exception.getMessage(); + if (TextUtils.isEmpty(message)) { + message = mErrorMessage.apiErrorMessage((ApiErrorException) exception); } } @@ -74,4 +80,4 @@ public class ErrorMessageFactory { return str == null || str.toString().trim().length() == 0; } -} +} \ No newline at end of file