NetworkOnMainThreadException在Facebook上登录使用的PhoneGa

2019-06-24 19:06发布

我目前正在创建一个使用PhoneGap的1.6.0和煎茶触摸1.1的应用程序。 近日,采用Android 4.0.4智能手机进行测试时,我们发现,Facebook的登录并不如预期那么回事。 我们使用位于PhoneGap的Facebook的插件在这里用补丁科尔多瓦。 什么情况是,一旦我们尝试登录到Facebook上与Android蜂窝或更高的智能手机,应用程序启动一个NetworkOnMainThreadException因为网络操作不是在主线程上允许的。 此异常只扔在Android蜂窝或以上,这就是为什么我们没有注意到它在2.3.3。 调用的FB.login时抛出异常(),并没有达到回调。

我已经检查各个线程和主题,但都是极其模糊。 我知道,我必须在一个单独的线程中运行的网络操作,但我不能确定如何应该这样做 。 线程似乎在Java中发生,但我没有使用任何Java可言,所以,这不是我写的代码瞎搞周围之前,我想一些帮助。

我会尽量缩小以后哪一部分会导致异常。 任何帮助,将不胜感激。

错误堆栈:

05-25 15:42:29.018: I/Web Console(3785): Cordova Facebook Connect plugin initialized successfully. at file:///android_asset/www/cdv-plugin-fb-connect.js:24
05-25 15:42:29.120: D/OpenGLRenderer(3785): Flushing caches (mode 0)
05-25 15:42:30.479: D/ConnectPlugin(3785): authorized
05-25 15:42:30.479: D/ConnectPlugin(3785): Bundle[{expires_in=5177550, access_token=censored, code=censored}]
05-25 15:42:30.503: D/AndroidRuntime(3785): Shutting down VM
05-25 15:42:30.503: W/dalvikvm(3785): threadid=1: thread exiting with uncaught exception (group=0x40a671f8)
05-25 15:42:30.511: E/AndroidRuntime(3785): FATAL EXCEPTION: main
05-25 15:42:30.511: E/AndroidRuntime(3785): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=32665, result=-1, data=Intent { (has extras) }} to activity {com.company.product/com.company.product.productActivity}: android.os.NetworkOnMainThreadException
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.app.ActivityThread.access$1100(ActivityThread.java:123)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.os.Looper.loop(Looper.java:137)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at java.lang.reflect.Method.invoke(Method.java:511)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at dalvik.system.NativeStart.main(Native Method)
05-25 15:42:30.511: E/AndroidRuntime(3785): Caused by: android.os.NetworkOnMainThreadException
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:664)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.io.Streams.readSingleByte(Streams.java:41)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:655)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpConnection.isStale(HttpConnection.java:256)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:71)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at com.facebook.android.Util.openUrl(Util.java:206)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at com.facebook.android.Facebook.request(Facebook.java:751)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at com.facebook.android.Facebook.request(Facebook.java:688)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at org.apache.cordova.facebook.ConnectPlugin$AuthorizeListener.onComplete(ConnectPlugin.java:271)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at com.facebook.android.Facebook.authorizeCallback(Facebook.java:433)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at org.apache.cordova.facebook.ConnectPlugin.onActivityResult(ConnectPlugin.java:196)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:1178)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.app.Activity.dispatchActivityResult(Activity.java:4649)
05-25 15:42:30.511: E/AndroidRuntime(3785):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
05-25 15:42:30.511: E/AndroidRuntime(3785):     ... 11 more

Answer 1:

我设法把认证的一部分,解决了错误ConnectPlugin.java在一个单独的线程。 对于未来的读者,我将发布以下说明。

ConnectPlugin.java ,更换:

try {
    JSONObject o = new JSONObject(this.fba.facebook.request("/me"));
    this.fba.userId = o.getString("id");
    this.fba.success(getResponse(), this.fba.callbackId);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

用下面的代码:

Thread t = new Thread(new Runnable() {
    public void run() {
        try {
            JSONObject o = new JSONObject(fba.facebook.request("/me"));
            fba.userId = o.getString("id");
            fba.success(getResponse(), fba.callbackId);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});
t.start();

这应该可以解决NetworkOnMainThreadException



文章来源: NetworkOnMainThreadException on Facebook Login with Phonegap 1.6.0