好的我有一个API,它返回包含两个字段的json
public interface AuthClient {
@Headers({"Content-Type: application/json", "Accept: application/json", "deviceType: android"})
@POST("api/v3.2/auth")
Call<AuthResp> userLogin(@Body String body);
}
当我使用带有此AuthResp的Call时,表示json响应改型的模型类给出了错误代码400和错误消息:: {&#34; errors&#34;:&#34;(not(map?a) -java.lang.String))&#34;}
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://172.16.10.196:3000/")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(OkHttpClientProvider.getInstance().getClient())
.build();
String json1=new Gson().toJson(new User(8,"123456789"));
Call<AuthResp> call=retrofit.create(AuthClient.class).userLogin(json1);
call.enqueue(new Callback<AuthResp>() {
@Override
public void onResponse(Call<AuthResp> call, Response<AuthResp> response) {
try {
if(response.isSuccessful()) {
Log.d("Response Success","True");
Log.d("Response", response.body().refreshToken);
}
else{
Log.d("Response Not Success",response.code()+""+response.errorBody().string());
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<AuthResp> call, Throwable t) {
Log.d("Response","Failed" +t.getMessage());
}
});
}
我确定没有丢失的标题或错误的参数,因为如果我使用ResponseBody进行调用,然后使用Gson将字符串转换为AuthResp,它就像魅力一样。
使用的改装版本::
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.+
implementation 'com.squareup.retrofit2:converter-scalars:2.+'
implementation 'com.squareup.retrofit2:converter-gson:2.+'
AuthResp Class
public class AuthResp {
public String token;
public String refreshToken;
public AuthResp(){
}
public AuthResp(String token,String refreshToken){
this.token=token;
this.refreshToken=refreshToken;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
}
来自服务器的响应
{
"token": "some string",
"refreshToken": "some string"
}
任何人都可以告诉我问题可能是什么吗?