java.lang.IllegalArgumentException: 'retrofit2.Response' is not a valid response body type. Did you mean ResponseBody? for method Api.NewLocation

Viewed 957

I am trying to set dummy data to be sent using my API but unfortunately, I cannot define the error in my logCat.

Call<Response> call=RetrofitClient
                                .getmInstance()
                                .getApi()
                                .NewLocation("357040068483739","16.623463660","126.007236650",""+"2018-05-07 11:28:02","0","9090");

                                call.enqueue(new Callback<Response>() {
                                    @Override
                                    public void onResponse(Call<Response> call, Response<Response> response) {
                                        Toast.makeText(NavDrawerFleet.this, "Successful", Toast.LENGTH_SHORT).show();
                                              }

                                    @Override
                                    public void onFailure(Call<Response> call, Throwable t) {
                                        Toast.makeText(NavDrawerFleet.this`enter code here`, "Data Not Send", Toast.LENGTH_SHORT).show();
                                    }
                                });

This is my API code.

public interface Api {

    @FormUrlEncoded
    @POST("/api/Database/NewLocation")
    Call<Response>NewLocation(
        @Field("SerialNumber")String serialNumber,
        @Field("Coordinate1")String latitude,
        @Field("Coordinate2")String longitude,
        @Field("DateTime")String dateTime,
        @Field("Speed")String speed,
        @Field("Port")String port


    );
    @FormUrlEncoded
    @POST("/api/Registration/RegisterDevice")
    Call<Response> NewRegistered(
        @Field("SerialNumber")String serialNumber,
        @Field("FirstName") String firstName,
        @Field("LastName") String lastName,
        @Field("Status") String status
    );
}

And this is my retrofitClient class; I have been watching the new implementation of retrofit2 but when testing in my app it produces that error and I cannot determine it.

public class RetrofitClient {
    private static  final  String BASE_URL="http://58.69.149.164:9114";
    private static RetrofitClient mInstance;
    private Retrofit retrofit;

    private RetrofitClient() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public static  synchronized  RetrofitClient getmInstance(){
        if(mInstance==null){
            mInstance=new RetrofitClient();
        }
    return mInstance;
    }
    public Api getApi(){
        return  retrofit.create(Api.class);
    }
1 Answers

That error/warning is pretty self explanatory IMO. In your API you're using Retrofit's Response as return type instead of ResponseBody. Change the API class like below:

/* other imports*/
import okhttp3.ResponseBody;

public interface Api {

    @FormUrlEncoded
    @POST("/api/Database/NewLocation")
    Call<ResponseBody> NewLocation(     /* use Call<ResponseBody> */
        @Field("SerialNumber")String serialNumber,
        @Field("Coordinate1")String latitude,
        @Field("Coordinate2")String longitude,
        @Field("DateTime")String dateTime,
        @Field("Speed")String speed,
        @Field("Port")String port
    );

    @FormUrlEncoded
    @POST("/api/Registration/RegisterDevice")
    Call<ResponseBody> NewRegistered(      /* use Call<ResponseBody> */
        @Field("SerialNumber")String serialNumber,
        @Field("FirstName") String firstName,
        @Field("LastName") String lastName,
        @Field("Status") String status
    );
}

And use it like below:

Call<ResponseBody> call = RetrofitClient
                            .getmInstance()
                            .getApi()
                            .NewLocation(/* paramas */);

call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        // do whatever you want
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("tag", "Error ", t);
    }
});

And don't forget to import okhttp3.ResponseBody; wherever you use ResponseBody class.

Related