optimize code

androidx
Ztiany 5 years ago
parent e9f474a156
commit f8acbd27ac
  1. 10
      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. 26
      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();
}
} 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 _______");
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();
}
}

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

@ -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,43 +26,47 @@ public class ErrorMessageFactory {
ErrorMessage mErrorMessage = NetContext.get().netProvider().errorMessage();
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;
//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) {
//4:Api Error
else if (exception instanceof ApiErrorException) {
message = exception.getMessage();
if (TextUtils.isEmpty(message)) {
message = mErrorMessage.apiErrorMessage((ApiErrorException) exception);
}
} else {
throw new RuntimeException(exception);
}
}
if (isEmpty(message)) {

Loading…
Cancel
Save