@@ -1,5 +1,6 @@ |
||
| 1 | 1 |
package ai.pai.lensman.box; |
| 2 | 2 |
|
| 3 |
+import com.google.gson.Gson; |
|
| 3 | 4 |
import com.google.gson.GsonBuilder; |
| 4 | 5 |
|
| 5 | 6 |
import java.io.IOException; |
@@ -25,10 +26,12 @@ public final class ApiClient {
|
||
| 25 | 26 |
.addInterceptor(createHttpLoggingInterceptor()) |
| 26 | 27 |
.build(); |
| 27 | 28 |
|
| 29 |
+ public static final Gson gson = new GsonBuilder().create(); |
|
| 30 |
+ |
|
| 28 | 31 |
public static final ApiService service = new Retrofit.Builder() |
| 29 | 32 |
.baseUrl(BASE_URL) |
| 30 | 33 |
.client(client) |
| 31 |
- .addConverterFactory(GsonConverterFactory.create( new GsonBuilder().create())) |
|
| 34 |
+ .addConverterFactory(GsonConverterFactory.create(gson)) |
|
| 32 | 35 |
.build() |
| 33 | 36 |
.create(ApiService.class); |
| 34 | 37 |
|
@@ -0,0 +1,17 @@ |
||
| 1 |
+package ai.pai.lensman.box; |
|
| 2 |
+ |
|
| 3 |
+import retrofit2.Response; |
|
| 4 |
+ |
|
| 5 |
+public interface CallbackLifecycle<T> {
|
|
| 6 |
+ |
|
| 7 |
+ boolean onResultOk(Response<T> response, T result); |
|
| 8 |
+ |
|
| 9 |
+ boolean onResultError(Response<T> response, Result.Error error); |
|
| 10 |
+ |
|
| 11 |
+ boolean onCallCancel(); |
|
| 12 |
+ |
|
| 13 |
+ boolean onCallException(Throwable t, Result.Error error); |
|
| 14 |
+ |
|
| 15 |
+ void onFinish(); |
|
| 16 |
+ |
|
| 17 |
+} |
@@ -0,0 +1,102 @@ |
||
| 1 |
+package ai.pai.lensman.box; |
|
| 2 |
+ |
|
| 3 |
+import android.support.annotation.NonNull; |
|
| 4 |
+ |
|
| 5 |
+import com.google.gson.JsonSyntaxException; |
|
| 6 |
+import com.google.gson.annotations.SerializedName; |
|
| 7 |
+ |
|
| 8 |
+import java.io.IOException; |
|
| 9 |
+import java.net.ConnectException; |
|
| 10 |
+import java.net.SocketTimeoutException; |
|
| 11 |
+import java.net.UnknownHostException; |
|
| 12 |
+ |
|
| 13 |
+import retrofit2.Response; |
|
| 14 |
+ |
|
| 15 |
+public class Result {
|
|
| 16 |
+ |
|
| 17 |
+ protected boolean success; |
|
| 18 |
+ |
|
| 19 |
+ public boolean isSuccess() {
|
|
| 20 |
+ return success; |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ public static class Data<T> extends Result {
|
|
| 24 |
+ |
|
| 25 |
+ private T data; |
|
| 26 |
+ |
|
| 27 |
+ public T getData() {
|
|
| 28 |
+ return data; |
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ } |
|
| 32 |
+ |
|
| 33 |
+ public static class Error extends Result {
|
|
| 34 |
+ |
|
| 35 |
+ @SerializedName("error_msg")
|
|
| 36 |
+ private String errorMessage; |
|
| 37 |
+ |
|
| 38 |
+ public String getErrorMessage() {
|
|
| 39 |
+ return errorMessage; |
|
| 40 |
+ } |
|
| 41 |
+ |
|
| 42 |
+ } |
|
| 43 |
+ |
|
| 44 |
+ public static <T extends Result> Error buildError(@NonNull Response<T> response) {
|
|
| 45 |
+ try {
|
|
| 46 |
+ return ApiClient.gson.fromJson(response.errorBody().string(), Error.class); |
|
| 47 |
+ } catch (IOException | JsonSyntaxException e) {
|
|
| 48 |
+ Error error = new Error(); |
|
| 49 |
+ error.success = false; |
|
| 50 |
+ switch (response.code()) {
|
|
| 51 |
+ case 400: |
|
| 52 |
+ error.errorMessage = "请求参数有误"; |
|
| 53 |
+ break; |
|
| 54 |
+ case 403: |
|
| 55 |
+ error.errorMessage = "请求被拒绝"; |
|
| 56 |
+ break; |
|
| 57 |
+ case 404: |
|
| 58 |
+ error.errorMessage = "资源未找到"; |
|
| 59 |
+ break; |
|
| 60 |
+ case 405: |
|
| 61 |
+ error.errorMessage = "请求方式不被允许"; |
|
| 62 |
+ break; |
|
| 63 |
+ case 408: |
|
| 64 |
+ error.errorMessage = "请求超时"; |
|
| 65 |
+ break; |
|
| 66 |
+ case 422: |
|
| 67 |
+ error.errorMessage = "请求语义错误"; |
|
| 68 |
+ break; |
|
| 69 |
+ case 500: |
|
| 70 |
+ error.errorMessage = "服务器逻辑错误"; |
|
| 71 |
+ break; |
|
| 72 |
+ case 502: |
|
| 73 |
+ error.errorMessage = "服务器网关错误"; |
|
| 74 |
+ break; |
|
| 75 |
+ case 504: |
|
| 76 |
+ error.errorMessage = "服务器网关超时"; |
|
| 77 |
+ break; |
|
| 78 |
+ default: |
|
| 79 |
+ error.errorMessage = response.message(); |
|
| 80 |
+ break; |
|
| 81 |
+ } |
|
| 82 |
+ return error; |
|
| 83 |
+ } |
|
| 84 |
+ } |
|
| 85 |
+ |
|
| 86 |
+ public static Error buildError(@NonNull Throwable t) {
|
|
| 87 |
+ Error error = new Error(); |
|
| 88 |
+ error.success = false; |
|
| 89 |
+ if (t instanceof UnknownHostException || t instanceof ConnectException) {
|
|
| 90 |
+ error.errorMessage = "网络无法连接"; |
|
| 91 |
+ } else if (t instanceof SocketTimeoutException) {
|
|
| 92 |
+ error.errorMessage = "网络访问超时"; |
|
| 93 |
+ } else if (t instanceof JsonSyntaxException) {
|
|
| 94 |
+ error.errorMessage = "响应数据格式错误"; |
|
| 95 |
+ } else {
|
|
| 96 |
+ error.errorMessage = "未知错误:" + t.getLocalizedMessage(); |
|
| 97 |
+ } |
|
| 98 |
+ return error; |
|
| 99 |
+ } |
|
| 100 |
+ |
|
| 101 |
+ |
|
| 102 |
+} |