2017-04-24 94 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

不客气。干杯! :) –

相关问题