2017-01-25 2 views
0

데이터베이스에 토큰을 보내려고합니다. 그러나 내 데이터베이스는 토큰을받지 못합니다. 최종 OkHttpClient 클라이언트 = 새로운 OkHttpClient()가 스레딩 문제를 가지고 있다는 것을 알았습니다. 이 문제를 해결하는 방법? 최신 okhttp3 및 okio jar 파일을 사용하고 있습니다.최종 OkHttpClient 클라이언트에서의 멀티 스레딩 문제 = 새 OkHttpClient();

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 : GLSUser/W (2025) : 때 java.io.IOException 잘못된 장치 응답 키는 입니다. 01-26 05 : 16 : 09.564 : W/GLSUser (2025) : 16 : 09.564 :

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에서 (2025) GLSUser/W : kmo.run (AT : 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) : ktt.run (: 2015)에서 W/GLSUser (2025) : W/GLSUser (2025) com// GLSUser (2025) : java.lang.Thread.run (Thread.java:761) 01-26 05:16 : 09.572 : W/ContentTaskController (2025) : Invali d newTask는 시작 추적에 제공된
입니다.

답변

0

먼저 로깅을 추가하여 무슨 일이 일어나고 있는지 더 잘 파악하십시오. 예를 들어 Firebase에서 토큰을 가져 왔습니까? 나는 의심하지 않는다.

두 번째로 okhttp으로 비동기 호출을 만들려면 스레드 인스턴스를 직접 만들지 말고 out.enqueue(..callback..) [okhttp 참조]을 선택하십시오. 훨씬 적은 번거 로움, 당신을 위해 처리 된 모든 어려운 경우.

그러나이 특별한 경우에는 Firebase 토큰을 서버에 보내도록 별도의 IntentService를 만드는 것이 좋습니다. 이렇게하면 시스템 호출에서 호출 시간, 오류 처리 등이 onTokenRefresh()으로 분리됩니다. IntentService onHandleIntent() 안에 okhttp을 사용하여 동기식 전화를 걸 수 있습니다. 모든 것을 좋고 단순하게 유지합니다.