Java 最终OkHttpClient=新OkHttpClient()的多线程问题;
我正在尝试将令牌发送到我的数据库。但是,我的数据库没有收到任何令牌。我发现最终的OkHttpClient=new OkHttpClient()存在线程问题。如何解决这个问题?我正在使用最新的okhttp3和okiojar文件 MyFireBaseInstancedService.javaJava 最终OkHttpClient=新OkHttpClient()的多线程问题;,java,android,multithreading,firebase,okhttp3,Java,Android,Multithreading,Firebase,Okhttp3,我正在尝试将令牌发送到我的数据库。但是,我的数据库没有收到任何令牌。我发现最终的OkHttpClient=new OkHttpClient()存在线程问题。如何解决这个问题?我正在使用最新的okhttp3和okiojar文件 MyFireBaseInstancedService.java package ******; import android.util.Log; import android.widget.Toast; import com.google.firebase.iid.Fi
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):在
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):在kmo.run(:com.google.android.gms:450)
01-26 05:16:09.564:W/GLSUser(2025):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
01-26 05:16:09.564:W/GLSUser(2025):位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
01-26 05:16:09.564:W/GLSUser(2025):在kqt.run(:com.google.android.gms:17)
01-26 05:16:09.564:W/GLSUser(2025):在java.lang.Thread.run(Thread.java:761)
01-26 05:16:09.572:W/ContentTaskController(2025):无效的新任务was提供给startTracking。首先,添加一些日志以更好地了解正在发生的事情。例如,您是否曾经从Firebase获得令牌?我想不是 其次,不要创建自己的线程实例来使用
okhttp
进行异步调用,请检查out.enqueue(…callback..)
[请参阅。更省事的是,所有困难的边缘情况都会为您处理
但是,在这种特殊情况下,我建议创建一个单独的IntentService,将Firebase令牌发送到服务器。这将使调用持续时间、错误处理等与对onTokenRefresh()
的系统调用分离。在IntentServiceonHandleIntent()内部
然后,您可以使用okhttp
进行同步调用。让一切都变得简单而美好