optimize code

androidx
Ztiany 5 years ago
parent e9f474a156
commit f8acbd27ac
  1. 16
      lib_base/src/main/java/com/android/base/utils/android/DevicesUtils.java
  2. 2
      lib_base/src/main/java/com/android/base/utils/android/views/SizeEx.kt
  3. 34
      lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java

@ -64,7 +64,7 @@ public class DevicesUtils {
serial = Build.class.getField("SERIAL").get(null).toString(); serial = Build.class.getField("SERIAL").get(null).toString();
} }
} catch (Exception e) { } catch (Exception e) {
Timber.e(e,"getDeviceId"); Timber.w("getDeviceId error: " + e.getMessage());
} }
if (serial == null) { if (serial == null) {
//serial 需要一个初始化 //serial 需要一个初始化
@ -74,9 +74,8 @@ public class DevicesUtils {
return new UUID(M_SZ_DEV_ID_SHORT.hashCode(), serial.hashCode()).toString(); return new UUID(M_SZ_DEV_ID_SHORT.hashCode(), serial.hashCode()).toString();
} }
@SuppressLint("ObsoleteSdkInt") @SuppressLint("ObsoleteSdkInt")
public static String printSystemInfo() { public static void printSystemInfo() {
Date date = new Date(System.currentTimeMillis()); Date date = new Date(System.currentTimeMillis());
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(date); String time = dateFormat.format(date);
@ -117,11 +116,14 @@ public class DevicesUtils {
} }
sb.append("\n_______ GINGERBREAD-9 _______"); sb.append("\n_______ GINGERBREAD-9 _______");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { try {
sb.append("\nSERIAL :").append(Build.SERIAL); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
sb.append("\nSERIAL :").append(Build.SERIAL);
}
} catch (Exception ignore) {
} }
Log.i("DEVICES", sb.toString()); Log.i("DEVICES", sb.toString());
return sb.toString();
} }
} }

@ -1,3 +1,5 @@
@file:JvmName("Sizes")
package com.android.base.utils.android.views package com.android.base.utils.android.views
import android.util.TypedValue import android.util.TypedValue

@ -9,7 +9,9 @@ import com.android.sdk.net.exception.ServerErrorException;
import com.android.sdk.net.provider.ErrorMessage; import com.android.sdk.net.provider.ErrorMessage;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import io.reactivex.exceptions.CompositeException;
import retrofit2.HttpException; import retrofit2.HttpException;
import timber.log.Timber; import timber.log.Timber;
@ -24,42 +26,46 @@ public class ErrorMessageFactory {
ErrorMessage mErrorMessage = NetContext.get().netProvider().errorMessage(); ErrorMessage mErrorMessage = NetContext.get().netProvider().errorMessage();
Timber.d("createMessage with:%s", exception.toString()); Timber.d("createMessage with:%s", exception.toString());
//handle rx CompositeException
if (exception instanceof CompositeException) {
List<Throwable> exceptions = ((CompositeException) exception).getExceptions();
if (exceptions != null && !exceptions.isEmpty()) {
exception = exceptions.get(0);
}
}
CharSequence message = null; CharSequence message = null;
//SocketTimeoutException android NetworkErrorException extends IOException //SocketTimeoutException, NetworkErrorException extends IOException
//1:网络连接错误处理 //1:网络连接错误处理
if (exception instanceof IOException || exception instanceof NetworkErrorException) { if (exception instanceof IOException || exception instanceof NetworkErrorException) {
message = mErrorMessage.netErrorMessage(exception); message = mErrorMessage.netErrorMessage(exception);
} }
//2:服务器错误处理 //2:服务器错误处理
else if (exception instanceof ServerErrorException) { else if (exception instanceof ServerErrorException) {
int errorType = ((ServerErrorException) exception).getErrorType(); int errorType = ((ServerErrorException) exception).getErrorType();
if (errorType == ServerErrorException.SERVER_DATA_ERROR) { if (errorType == ServerErrorException.SERVER_DATA_ERROR) {
message = mErrorMessage.serverDataErrorMessage(exception); message = mErrorMessage.serverDataErrorMessage(exception);
} else if (errorType == ServerErrorException.UNKNOW_ERROR) { } else if (errorType == ServerErrorException.UNKNOW_ERROR) {
message = mErrorMessage.serverErrorMessage(exception); message = mErrorMessage.serverErrorMessage(exception);
} }
} }
//3:响应码非200处理 //3:响应码非200处理
else if (exception instanceof HttpException) { else if (exception instanceof HttpException) {
int code = ((HttpException) exception).code(); int code = ((HttpException) exception).code();
if (code >= 500/*http 500 表示服务器错误*/) { if (code >= 500/*http 500 表示服务器错误*/) {
message = mErrorMessage.serverErrorMessage(exception); message = mErrorMessage.serverErrorMessage(exception);
} else if (code >= 400/*http 400 表示客户端请求出错*/) { } else if (code >= 400/*http 400 表示客户端请求出错*/) {
message = mErrorMessage.clientRequestErrorMessage(exception); message = mErrorMessage.clientRequestErrorMessage(exception);
} }
}
} else { //4:Api Error
//4:api 错误处理 else if (exception instanceof ApiErrorException) {
if (exception instanceof ApiErrorException) { message = exception.getMessage();
message = exception.getMessage(); if (TextUtils.isEmpty(message)) {
if (TextUtils.isEmpty(message)) { message = mErrorMessage.apiErrorMessage((ApiErrorException) exception);
message = mErrorMessage.apiErrorMessage((ApiErrorException) exception);
}
} else {
throw new RuntimeException(exception);
} }
} }
@ -74,4 +80,4 @@ public class ErrorMessageFactory {
return str == null || str.toString().trim().length() == 0; return str == null || str.toString().trim().length() == 0;
} }
} }
Loading…
Cancel
Save