简体   繁体   中英

Retrofit returns status 400 when using custom class with Call

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<AuthResp> 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://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());
        }
    });
}

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 ?

Add ScalarsConverterFactory

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://172.16.10.196:3000/")
        .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create(gson))
        .client(OkHttpClientProvider.getInstance().getClient())
        .build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM