【发布时间】:2019-06-17 19:38:35
【问题描述】:
我正在使用带有 OkHttp 的 Retrofit2。除了一件事 - 应用程序关闭或在后台时的网络调用之外,一切都可以正常工作。
工作
每当 Firebase 通知到达时,我都会进行网络调用以获取与该通知相关的数据,然后显示通知。
场景
考虑一个在后台或关闭并收到通知的 Android 应用。
发生了什么
通知成功到达,我可以看到。但是当我尝试拨打电话时,总是报错Failed to connect to: *******,而且我在服务器上看不到任何呼叫。
我见过一些类似的问题,但没有一个能帮助我。 这是一个列表:
- https://github.com/square/okhttp/issues/1771
- https://github.com/square/okhttp/issues/3146
- https://github.com/square/okhttp/issues/1037
- https://github.com/square/okhttp/issues/1792
有些人建议使用connectionPool,有些人建议使用pingInterval。在我的情况下没有任何效果。不知道是Retrofit还是OkHttp的bug。
下面是我用于调用的一段代码。请记住,当应用程序在前台时它工作得非常好。
OkHttpClient.Builder builder = getHttpBuilder()
.connectionSpecs(getSpecList());
retrofit = new Retrofit.Builder()
.baseUrl(Session.getBaseUrl())
.client(builder.build())
.build();
Call<ResponseBody> call = retrofit
.create(Webservice.class)
.pullNotify(authToken, ivB64, digest, Session.getTimestamp());
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
listener.onNetworkCallSuccess(response);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
listener.onNetworkCallFailure(t);
}
});
它总是返回SocketTimeoutException。任何帮助/指导表示赞赏。
【问题讨论】:
标签: android retrofit retrofit2 okhttp