77 lines
2.7 KiB
Java
77 lines
2.7 KiB
Java
package com.ubtrobot.retrofit.adapter.urest;
|
|
|
|
import com.ubt.jimu.unity.bluetooth.UnityActivity;
|
|
import com.ubtrobot.http.rest.URestException;
|
|
import java.io.IOException;
|
|
import java.lang.annotation.Annotation;
|
|
import java.net.ConnectException;
|
|
import java.net.SocketException;
|
|
import java.net.SocketTimeoutException;
|
|
import retrofit2.Call;
|
|
import retrofit2.Response;
|
|
import retrofit2.Retrofit;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class URestCall<T> {
|
|
private final Retrofit a;
|
|
private final Call<T> b;
|
|
private volatile Response<T> c;
|
|
|
|
private static class ErrorMessage {
|
|
public int a;
|
|
public String b;
|
|
|
|
private ErrorMessage() {
|
|
}
|
|
}
|
|
|
|
URestCall(Retrofit retrofit, Call<T> call) {
|
|
this.a = retrofit;
|
|
this.b = call;
|
|
}
|
|
|
|
private void b() {
|
|
this.c = null;
|
|
}
|
|
|
|
public T a() throws URestException {
|
|
b();
|
|
try {
|
|
this.c = this.b.execute();
|
|
if (this.c.isSuccessful()) {
|
|
return this.c.body();
|
|
}
|
|
throw a(this.c);
|
|
} catch (IOException e) {
|
|
throw a(e);
|
|
}
|
|
}
|
|
|
|
private URestException a(Response<T> response) {
|
|
try {
|
|
ErrorMessage errorMessage = (ErrorMessage) this.a.responseBodyConverter(ErrorMessage.class, new Annotation[0]).convert(response.errorBody());
|
|
if (errorMessage != null && errorMessage.a > 0) {
|
|
return new URestException(errorMessage.a, errorMessage.b);
|
|
}
|
|
return new URestException(UnityActivity.CODE_BROWSE_PROGRAM, "Illegal error message response. Should be \"{\"code\": XXYY, \"message\": \"An error message\"}\"(XXYY is a positive integer.)");
|
|
} catch (IOException e) {
|
|
return new URestException(UnityActivity.CODE_BROWSE_PROGRAM, "Illegal error message response. Should be \"{\"code\": XXYY, \"message\": \"An error message\"}\"(XXYY is a positive integer.)", e);
|
|
}
|
|
}
|
|
|
|
private URestException a(Throwable th) {
|
|
if (th instanceof SocketException) {
|
|
if (th instanceof ConnectException) {
|
|
return new URestException(-2, th.getMessage(), th);
|
|
}
|
|
if (th.getMessage() != null && th.getMessage().contains("Permission denied")) {
|
|
return new URestException(-1, "Network permission denied. Add \"<uses-permission android:name=\"android.permission.INTERNET\" />\" in your AndroidManifest.xml", th);
|
|
}
|
|
}
|
|
if (th instanceof SocketTimeoutException) {
|
|
return new URestException(-3, "Socket timeout.", th);
|
|
}
|
|
return new URestException(-4, "Internal HTTP client error. Please contact the author to fix it.", th);
|
|
}
|
|
}
|