Has*_*aza 5 android json retrofit2
我从API调用中收到一个正文,但onResponse()没有被调用,这里是方法:
final Rest_manager_league rest = new Rest_manager_league();
Call<List<Root>> listCall = rest.getMLeague_conn().getLeague(x);
listCall.enqueue(new Callback<List<Root>>() {
@Override
public void onResponse(Call<List<Root>> call, Response<List<Root>> response) {
lg = response.body();
Log.d("res", "ON");
if (response.isSuccessful()){
textView.setText(lg.get(3).getStanding().get(2).getTeamName());
Log.d("s", "true");
}
}
@Override
public void onFailure(Call<List<Root>> call, Throwable t) {
Log.d("Failure", "Failed");
}
});
Run Code Online (Sandbox Code Playgroud)
这是Retrofit界面和服务:
public interface league_Conn {
@GET("/v1/soccerseasons/{id}/leagueTable")
@Headers("X-Auth-Token:" +
"1869f69f772b40a2a12fd6eefb4e48ef ")
Call<List<Root>> getLeague(@Path("id") int id);
}
public class Rest_manager_league {
private league_Conn mleague_conn;
public league_Conn getMLeague_conn() {
if (mleague_conn == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.football-data.org/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
mleague_conn = retrofit.create(league_Conn.class);
}
return mleague_conn;
}
}
Run Code Online (Sandbox Code Playgroud)
在logcat中,onFailure()正在出现.像这样:
okhttp3 <-- END HTTP (8300 byte body) Failer :Failed
为什么onResponse()不被叫?
您正在获取响应正文(8300 字节),但onFailure被调用,因为您返回的正文与您的GSONFactory. 反序列化过程不起作用。您可以通过打印堆栈跟踪来查明问题,正如 @yazan 指出的那样。只需输入:
t.printStackTrace()
Run Code Online (Sandbox Code Playgroud)
在onFailure()。
编辑:
发生该错误是因为您告诉 Retrofit 您需要一个列表,但您得到的是一个 JSON 对象。我快速查看了您正在使用的 API,看起来它返回一个 JSON 对象,并且返回的对象包含您有兴趣访问的列表。尝试List<Root>将的实例替换为Root. 如需更多帮助,您还可以查看这个问题:
GSON 抛出预期的 BEGIN_ARRAY 但实际是 BEGIN_OBJECT 错误
| 归档时间: |
|
| 查看次数: |
1588 次 |
| 最近记录: |