永远不会调用Retrofit 2 onResponse方法

问题描述 投票:2回答:1

首先我知道这个问题已经被多次提出过了。但我无法找到问题的解决方案。从日志中我可以看到Web服务正确地将JSON返回给我。但由于某种原因,它永远不会进入onResponse方法。如果有人能就此事给我一个暗示,我将非常感激。

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

接口

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

}

记录输出

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)

T IPO event OD to.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;
    }
}
java android json retrofit retrofit2
1个回答
2
投票

我认为问题是你的pojo @SerializedName

请将你的@SerializedName("Id")改为@SerializedName("ID")@SerializedName("nombre")改为@SerializedName("Nombre")

让我知道它是否有效

© www.soinside.com 2019 - 2024. All rights reserved.