Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Retrofit returns status 400 when using custom class with Call

Omar El Hefny:

Ok I have an API that returns json that contains two fields


public interface AuthClient {
@Headers({"Content-Type: application/json", "Accept: application/json", "deviceType: android"})
@POST("api/v3.2/auth")
Call userLogin(@Body String body);
}

when I use the Call with this AuthResp which model class that represents the json response retrofit give me error code 400 and error message :: {"errors":"(not (map? a-java.lang.String))"}


Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://ift.tt/2AC0s2f")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(OkHttpClientProvider.getInstance().getClient())
.build();
String json1=new Gson().toJson(new User(8,"123456789"));
Call call=retrofit.create(AuthClient.class).userLogin(json1);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response 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 call, Throwable t) {
Log.d("Response","Failed" +t.getMessage());
}
});
}

I'm sure that there is no missing headers or wrong parameters because if I made the call with ResponseBody instead and then use Gson to convert the string to AuthResp it works like a charm.

retrofit versions used ::


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;
}

}

response from server


{
"token": "some string",
"refreshToken": "some string"
}

Can any one tell me what the problem might be ?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Retrofit returns status 400 when using custom class with Call

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×