gpt4 book ai didi

android - 在 Retrofit2 中接收响应主体但未调用 onResponse

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:42:25 25 4
gpt4 key购买 nike

我正在从我的 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");
}
});

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

在 logcat 中,onFailure()正在出现。像这样:

okhttp3 <-- END HTTP (8300 byte body) Failer :Failed

为什么是onResponse()没有接到电话?

最佳答案

您正在获得一个响应正文(8300 字节)但是 onFailure正在被调用,因为您返回的 body 与您的 GSONFactory 不一致.反序列化过程不起作用。正如@yazan 指出的那样,您可以通过打印堆栈跟踪来查明问题。只需输入:

t.printStackTrace()

onFailure() .

编辑:

错误的发生是因为您告诉 Retrofit 您需要一个列表,但您得到的是一个 JSON 对象。我快速查看了您正在使用的 API,它似乎返回了一个 JSON 对象,然后返回的对象包含您有兴趣访问的列表。尝试替换 List<Root> 的实例只是Root .如需更多帮助,您还可以查看此问题:

GSON throws Expected BEGIN_ARRAY but was BEGIN_OBJECT error

关于android - 在 Retrofit2 中接收响应主体但未调用 onResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37255562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com