Android, NetworkOnMainThreadException google drive

431 views Asked by At

I have problem with NetworkOnMainThreadException on Android.

I building a java app using Google API, . I work on java code as Google libs

developers.google.com/api-client-library/java/apis/drive/v2

This is my code

String code = data.getStringExtra("code");
        GoogleTokenResponse response;
        try {
            response = drive_flow.newTokenRequest(code).setRedirectUri(DRIVE_REDIRECT_URI).execute();
            GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setClientSecrets(DRIVE_CLIENT_ID, DRIVE_CLIENT_SECRET)
            .build()
            .setFromTokenResponse(response);
            //Create a new authorized API client
            drive_service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //catch (NetworkOnMainThreadException e){

        //}

(When i catch NetworkOnMainThreadException drive_service will be null)

My app run on AVD Platform 2.3.3 very good, but on 4.1.1 (on genymotion) it not work.

This is logcat:

11-17 02:53:37.190: E/AndroidRuntime(1408): FATAL EXCEPTION: main
11-17 02:53:37.190: E/AndroidRuntime(1408): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=999, result=999, data=Intent { (has extras) }} to activity {com.uit.testcloud3/com.uit.testcloud3.MainActivity}: android.os.NetworkOnMainThreadException
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.app.ActivityThread.access$1100(ActivityThread.java:130)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.os.Looper.loop(Looper.java:137)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at java.lang.reflect.Method.invoke(Method.java:511)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at dalvik.system.NativeStart.main(Native Method)
11-17 02:53:37.190: E/AndroidRuntime(1408): Caused by: android.os.NetworkOnMainThreadException
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at com.uit.testcloud3.MainActivity.onActivityResult(MainActivity.java:272)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.app.Activity.dispatchActivityResult(Activity.java:5192)
11-17 02:53:37.190: E/AndroidRuntime(1408):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
11-17 02:53:37.190: E/AndroidRuntime(1408):     ... 11 more
11-17 02:54:40.366: E/Trace(1462): error opening trace file: No such file or directory (2)
11-17 02:54:40.974: W/EGL_emulation(1462): eglSurfaceAttrib not implemented

My permission

<uses-permission android:name="android.permission.INTERNET" />

Thank for your reading and sorry for my bad english.

2

There are 2 answers

0
Blaze Tama On

Wrap this code :

    try {
        response = drive_flow.newTokenRequest(code).setRedirectUri(DRIVE_REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(jsonFactory)
        .setClientSecrets(DRIVE_CLIENT_ID, DRIVE_CLIENT_SECRET)
        .build()
        .setFromTokenResponse(response);
        //Create a new authorized API client
        drive_service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } //catch (NetworkOnMainThreadException e){

Inside a Thread or AsyncTask

1
Balvinder Singh On

Use this code in your Oncreate() Method

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
 .detectAll()
 .penaltyLog()
 .build();
 StrictMode.setThreadPolicy(policy);

or Use your Http calling inside AsyncTask.