2017-04-24 82 views
0

我嘗試使用Firebase平臺開發包含通知的簡單應用程序(名爲Noti)。我需要使用保存在我的服務器上的php腳本將令牌保存到數據庫中。問題是我按下按鈕後收到消息Noti has stopped調用registerToken時應用程序停止

package com.example.jaroslavvystavel.noti; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import com.google.firebase.iid.FirebaseInstanceId; 

import java.io.IOException; 

import okhttp3.FormBody; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.RequestBody; 

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = "MainActivity"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btnShowToken = (Button)findViewById(R.id.button_show_token); 
     btnShowToken.setOnClickListener(new View.OnClickListener() { 
      @Override 

      public void onClick(View v) { 
       //Get the token 
       String token = FirebaseInstanceId.getInstance().getToken(); 
       Log.d(TAG, "Token: " + token); 
       Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show(); 
       registerToken(token); 

      } 

     }); 

    } 
    private void registerToken(String token) { 

     OkHttpClient client = new OkHttpClient(); 
     RequestBody body = new FormBody.Builder() 
       .add("Token",token) 
       .build(); 

     Request request = new Request.Builder() 
       .url("http://www.stud.fit.vutbr.cz/~xvose02/testuji.php") 
       .post(body) 
       .build(); 

     try { 
      client.newCall(request).execute(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我從這個Youtube tutorial的靈感,其中一切工作正常。每次用戶按下按鈕時,我稍微修改了作者的代碼以調用registerToken方法。

的Android監視器日誌(從按下按鈕的那一刻):

04-24 05:42:58.601 28949-28949/com.example.jaroslavvystavel.noti D/MainActivity: Token: c7J9CHxblE8:APA91bEnZA6zU5nnzKVba19G5ViznKy5jR5-_arjavZWQcpLXsK4M5VOTxc50g1ANFSmFPJ-ADRXfhX-aDa3ZsRwkEhyrIFQXEJX5x3OcQDt7ejNMfF3Q8qeTSsmWbGB4fFxIe1QR0gc 
04-24 05:42:58.701 28949-28949/com.example.jaroslavvystavel.noti D/AndroidRuntime: Shutting down VM 
04-24 05:42:58.701 28949-28949/com.example.jaroslavvystavel.noti W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x417a1930) 
04-24 05:42:58.741 28949-28949/com.example.jaroslavvystavel.noti E/AndroidRuntime: FATAL EXCEPTION: main 
                        android.os.NetworkOnMainThreadException 
                         at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128) 
                         at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
                         at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
                         at java.net.InetAddress.getAllByName(InetAddress.java:214) 
                         at okhttp3.Dns$1.lookup(Dns.java:39) 
                         at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:170) 
                         at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:136) 
                         at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:81) 
                         at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:171) 
                         at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) 
                         at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) 
                         at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) 
                         at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
                         at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
                         at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) 
                         at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
                         at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
                         at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 
                         at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
                         at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) 
                         at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
                         at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
                         at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) 
                         at okhttp3.RealCall.execute(RealCall.java:69) 
                         at com.example.jaroslavvystavel.noti.MainActivity.registerToken(MainActivity.java:57) 
                         at com.example.jaroslavvystavel.noti.MainActivity.access$000(MainActivity.java:20) 
                         at com.example.jaroslavvystavel.noti.MainActivity$1.onClick(MainActivity.java:37) 
                         at android.view.View.performClick(View.java:4439) 
                         at android.widget.Button.performClick(Button.java:139) 
                         at android.view.View$PerformClick.run(View.java:18395) 
                         at android.os.Handler.handleCallback(Handler.java:725) 
                         at android.os.Handler.dispatchMessage(Handler.java:92) 
                         at android.os.Looper.loop(Looper.java:176) 
                         at android.app.ActivityThread.main(ActivityThread.java:5319) 
                         at java.lang.reflect.Method.invokeNative(Native Method) 
                         at java.lang.reflect.Method.invoke(Method.java:511) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                         at dalvik.system.NativeStart.main(Native Method) 

的AsyncTask類:

class updateDb extends AsyncTask<OkHttpClient, RequestBody, Request> { 

    private Exception exception; 



    @Override 
    protected Request doInBackground(String... params) { 
     try { 
      OkHttpClient client = new OkHttpClient(); 
      RequestBody body = new FormBody.Builder() 
        .add("Token",token) 
        .build(); 

      Request request = new Request.Builder() 
        .url("http://www.stud.fit.vutbr.cz/~xvose02/testuji.php") 
        .post(body) 
        .build(); 

      try { 
       client.newCall(request).execute(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      this.exception = e; 


     } 
     return null; 
    } 


} 

不知道關於方法的參數...

+0

嗨。你還可以發佈錯誤日誌嗎? –

+0

@AL。你好。你能告訴我在哪裏可以找到他們?對不起,我完全是Android開發的初學者。 – user7303261

+1

嗨。當然。日誌應該在Android監視器部分中可見。見[這裏](https://developer.android.com/studio/profile/android-monitor.html)。 –

回答

1

添加一個回答提供樣品代碼

請求看起來很好。其他參數並不是真的那麼必要。以下是我認爲代碼應該(簡化)的方式。

private void registerToken(final String token) { 

     new updateDb().execute(token); 

    } 

    private class updateDb extends AsyncTask<String, Void, Void> { 

     @Override 
     protected Void doInBackground(String... token) { 
      try { 
       OkHttpClient client = new OkHttpClient(); 
       RequestBody body = new FormBody.Builder() 
         .add("Token", token[0]) 
         .build(); 

       Request request = new Request.Builder() 
         .url("http://www.stud.fit.vutbr.cz/~xvose02/testuji.php") 
         .post(body) 
         .build(); 

       client.newCall(request).execute(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    } 
+0

是的!你救了我無眠的夜晚。非常感謝你。 – user7303261

+0

不客氣。乾杯! :) –

相關問題