应用程序在调用registerToken时停止

时间:2017-04-24 03:03:03

标签: android firebase firebase-cloud-messaging

我尝试使用Firebase平台开发包含通知的简单应用程序(名为Noti)。我需要使用保存在我服务器上的PHP脚本将令牌保存到数据库中。问题是我按下按钮后收到消息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(); } } }

registerToken

我受到Youtube tutorial的启发,一切正常。我只是稍微修改了作者的代码,每次用户按下按钮时调用 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) 方法。

Android Monitor日志(从按下按钮的那一刻起):

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;
    }


}

AsyncTask类:

{{1}}

不确定方法的参数......

1 个答案:

答案 0 :(得分: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;
        }
    }