Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 onResponse method is never called

first of all I know that this question has already been asked many times. But I cannot find a solution to the problem. From the log I can see that the web service correctly returns the JSON to me. But for some reason it never enters the onResponse method. I would be very grateful if someone could give me a hint on this matter.

public void getAllTipo_evento() {
        mTipo_eventoService.getAll().enqueue(new Callback<List<Tipo_eventoDTO>>() {
            @Override
            public void onResponse(Call<List<Tipo_eventoDTO>> call, Response<List<Tipo_eventoDTO>> response) {

                if(response.isSuccessful()) {
                    Log.d("MainActivity", "posts loaded from API");
                }else if(response.errorBody() != null){
                        try {
                            String errorBody = response.errorBody().string();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                else {
                    int statusCode  = response.code();
                    // handle request errors depending on status code
                }
            }

            @Override
            public void onFailure(Call<List<Tipo_eventoDTO>> call, Throwable t) {
                t.printStackTrace();
                Log.d("MainActivity", "error loading from API");

            }
        });
    }

RetrofitClient

public class RetrofitClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl){

        Log.d("RetrofitClient.LOGTAG", "HTTPClient");

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        if (retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

Interface

public interface Tipo_eventoService {
    @GET("tipo_evento/getAll")
    Call<List<Tipo_eventoDTO>> getAll();

}

Log output

D/RetrofitClient.LOGTAG: HTTPClient
D/RetrofitClient.LOGTAG: HTTPClient
D/OkHttp: --> GET http://10.0.2.2:3000/tipo_evento/getAll
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK http://10.0.2.2:3000/tipo_evento/getAll (63ms)
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Date: Sat, 16 Dec 2017 00:32:33 GMT
D/OkHttp: Content-Length: 286
D/OkHttp: [{"ID":1,"Nombre":"Shopping"},{"ID":2,"Nombre":"Night Life"},{"ID":3,"Nombre":"Fun and games"},{"ID":4,"Nombre":"Classes and workshops"},{"ID":5,"Nombre":"Food and beverage"},{"ID":6,"Nombre":"Concert and show"},{"ID":7,"Nombre":"Outdoor activity"},{"ID":8,"Nombre":"Wellness centres"}]
D/OkHttp: <-- END HTTP (286-byte body)

TipoEventoDTO.java

public class Tipo_eventoDTO {
    @SerializedName("Id")
    @Expose
    private int Id;
    @SerializedName("nombre")
    @Expose
    private String nombre;

    public Tipo_eventoDTO(int id, String nombre) {
        this.Id = id;
        this.nombre = nombre;
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        this.Id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
}
like image 339
Javier Coronel Avatar asked Oct 28 '22 21:10

Javier Coronel


1 Answers

I think the problem is your pojo @SerializedName s

Kindly change your @SerializedName("Id") to @SerializedName("ID") and @SerializedName("nombre") to @SerializedName("Nombre")

let me know if it works

like image 116
Murat Güç Avatar answered Nov 02 '22 04:11

Murat Güç