Java 从未调用改装2 onResponse方法
首先,我知道这个问题已经被问过很多次了。但我找不到解决问题的办法。从日志中,我可以看到web服务正确地将JSON返回给我。但由于某些原因,它从未进入onResponse方法。如果有人能在这件事上给我一个暗示,我将不胜感激Java 从未调用改装2 onResponse方法,java,android,json,retrofit,retrofit2,Java,Android,Json,Retrofit,Retrofit2,首先,我知道这个问题已经被问过很多次了。但我找不到解决问题的办法。从日志中,我可以看到web服务正确地将JSON返回给我。但由于某些原因,它从未进入onResponse方法。如果有人能在这件事上给我一个暗示,我将不胜感激 public void getAllTipo_evento() { mTipo_eventoService.getAll().enqueue(new Callback<List<Tipo_eventoDTO>>() {
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");
}
});
}
接口
public interface Tipo_eventoService {
@GET("tipo_evento/getAll")
Call<List<Tipo_eventoDTO>> getAll();
}
我认为问题在于你的pojo@SerializedName s 请将您的
@SerializedName(“Id”)
更改为@SerializedName(“Id”)
,@SerializedName(“nombre”)
更改为@SerializedName(“nombre”)
让我知道它是否有效你登录
[{“ID”:1,“Nombre”:“购物”},{“ID”:2,“Nombre”:“夜生活”},{“ID”:3,“Nombre”:“乐趣和游戏”},{“ID”:4,“Nombre”:“课堂和研讨会”},{“ID”:5,“Nombre”:“食品和饮料”},{“ID”:6,“Nombre”:“音乐会和表演”},{“ID”:7,“Nombre”:“户外活动”},{“ID”:8,“Nombre”:“健康中心”}]
没问题。有时Tipo\u eventoDTO
当类对象不匹配时,我认为解析有问题。能否调用execute()
而不是enqueue()
并检查输出?请尝试在“if(response.issusccessful())”行之前打印日志。您可能会收到响应,但由于分析错误,它会给出响应。isSuccessful()-->False请提供您的POJO类没有问题:)编码愉快!!此外,这对于创建POJOsI不知道的网站来说也是一件好事。我相信它会派上用场的。非常感谢你给我的帮助。
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)
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;
}
}