我的代码中的一切看起来都很好,但是我在response.errorBody()中遇到了关于OkHttp3错误的错误
这是我的api调用...
LoginApi loginApi = APIClient.getApiClient().create(LoginApi.class);
Call<Register> call = loginApi.signupApiCall(email, password);
Log.i("URLCalled", call.request().url().toString());
call.enqueue(new Callback<Register>() {
@Override
public void onResponse(@NonNull Call<Register> call, @NonNull Response<Register> response) {
try {
Log.i("ErrorSignup", Boolean.toString(response.body().getError());
}
catch (Exception e){
Log.i("ErrorSignup", e.getMessage());
}
我为响应分类了两个模型。 Register.java
public class Register {
@SerializedName("error")
@Expose
private boolean error;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private Data data;
public Register(boolean error, String message, Data data) {
this.error = error;
this.message = message;
this.data = data;
}
public boolean getError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
还有
还有data.java
public class Data {
@SerializedName("token")
@Expose
private String token;
@SerializedName("profile_id")
@Expose
private Integer id;
public Data(String token, Integer id) {
this.token = token;
this.id = id;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
我的Json回复看起来像这样
{
"error": "False",
"message": "User registered successfully",
"data": {
"token": "1f3d5a7baa2f4d89a58dc616025fe93a883b8b9c",
"profile_id": 6688
}
}
这是我的api接口。
//signup
@FormUrlEncoded
@POST("api/v1/users/register")
Call<Register> signupApiCall(@Field("email") String email,
@Field("password") String password);
我遇到这样的错误... okhttp3.ResponseBody$1@somerandomnumber
这是我的依赖项
//Retrofit
implementation('com.squareup.retrofit2:retrofit:2.6.1') {
// exclude Retrofit’s OkHttp dependency module and define your own module import
exclude module: 'okhttp'
}
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
这是我的APIClient
public class APIClient {
private static final String base_url = "myUrl";
private static Retrofit retrofit = null;
private static Gson gson = new GsonBuilder().setLenient().create();
private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(7, TimeUnit.MINUTES)
.readTimeout(7, TimeUnit.MINUTES)
.writeTimeout(7, TimeUnit.MINUTES)
.build();
public static Retrofit getApiClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
//scalar is for text and gson for json obect and arrays
retrofit=new Retrofit.Builder().baseUrl(base_url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit;
}
}