我正在尝试将令牌发送到我的数据库。但是,我的数据库没有收到任何令牌。我发现最终的新客户端= OkHttpClient OkHttpClient()有线程问题。如何解决这个问题?我使用的是最新的okhttp3和okio文件。
MyfirebaseInstanceIDService.java
package ******;
import android.util.Log;
import android.widget.Toast;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG =
MyFirebaseInstanceIDService.class.getSimpleName();
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String token = FirebaseInstanceId.getInstance().getToken();
sendToken(token);
}
private void sendToken(String token) {
final OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("token",token)
.build();
final okhttp3.Request request = new okhttp3.Request.Builder()
.url("******")
.post(body)
.build();
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}堆栈跟踪
01-26 05:16:09.564: W/GLSUser(2025): [AppCertManager] IOException while 请求密钥: 01-26 05:16:09.564: w/GLSUser(2025年):java.io.IOException:设备密钥响应无效。01-26 05:16:09.564: w/GLSUser(2025年):at
eka.a(:com.google.android.gms:271)
01-26 05:16:09.564: W/GLSUser(2025):
eka.a(:com.google.android.gms:4236)
01-26 05:16:09.564: W/GLSUser(2025): at ejz.a(:com.google.android.gms:46)
01-26 05:16:09.564: W/GLSUser(2025): at ejt.a(:com.google.android.gms:53)
01-26 05:16:09.564: W/GLSUser(2025): at ejs.a(:com.google.android.gms:111)
01-26 05:16:09.564: W/GLSUser(2025):at com.google.android.gms.auth.account.be.
legacy.AuthCronChimeraService.b
(:com.google.android.gms:4052)
01-26 05:16:09.564: W/GLSUser(2025):atdup.call(:com.google.android.gms:2043)
01-26 05:16:09.564: W/GLSUser(2025):a
java.util.concurrent.FutureTask.run(FutureTask.java:237)01-26 05:16:09.564: W/GLSUser(2025):at kmo.run(:com.google.android.gms:450) 01-26 05:16:09.564: W/GLSUser(2025):at GLSUser 01-26 05:16:09.564: W/GLSUser(2025年):at java.util.concurrent.ThreadPoolExecutor $Worker.run(ThreadPoolExecutor.java:607) 01-26 05:16:09.564: w/GLSUser(2025年):at kqt.run(:com.google.android.gms:17) 01-26 05:16:09.564: w/GLSUser(2025年):at java.lang.Thread.run(Thread.java:761)01-26 05:16:09.572: W/ContentTaskController(2025):无效的newTask
provided to startTracking.发布于 2017-02-08 17:53:07
首先,添加一些日志记录,以便更好地了解发生了什么。例如,您曾经从Firebase获得过令牌吗?我想不会。
其次,不要创建自己的线程实例来使用okhttp进行异步调用,请检查out.enqueue(..callback..) [参见okhttp。更少的麻烦,所有困难的边缘案例都为您处理。
然而,在这种特殊情况下,我建议创建一个单独的IntentService来将您的Firebase令牌发送到您的服务器。这将调用持续时间、错误处理等与对onTokenRefresh()的系统调用解耦。在您的IntentService onHandleIntent()中,您可以使用okhttp进行同步调用。让一切都变得简单和美好。
https://stackoverflow.com/questions/41858709
复制相似问题