1

I am developing an android app on google drive , and i want to get the username and space used in drive , and tried to follow the https://developers.google.com/drive/v2/reference/about/get to do so , i have tried the following code ,

private static void printAbout(Drive service) {
try {
  About about = service.about().get().execute();

  System.out.println("Current user name: " + about.getName());
  System.out.println("Root folder ID: " + about.getRootFolderId());
  System.out.println("Total quota (bytes): " + about.getQuotaBytesTotal());
  System.out.println("Used quota (bytes): " + about.getQuotaBytesUsed());
} catch (IOException e) {
  System.out.println("An error occurred: " + e);
}
  }

but , it returns error , and the log cat shows

   02-28 03:14:11.562: E/AndroidRuntime(10765): FATAL EXCEPTION: main
   02-28 03:14:11.562: E/AndroidRuntime(10765): java.lang.RuntimeException: Failure   delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.enterproid.dfgoogledrivefileextension/com.enterproid.file.activity.LoginActivity}: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at        android.app.ActivityThread.deliverResults(ActivityThread.java:3162)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3205)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.app.ActivityThread.access$1100(ActivityThread.java:138)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255 )
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.os.Looper.loop(Looper.java:213)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.app.ActivityThread.main(ActivityThread.java:4787 )
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at java.lang.reflect.Method.invokeNative(Native Method)
     02-28 03:14:11.562: E/AndroidRuntime(10765):   at java.lang.reflect.Method.invoke(Method.java:511)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at dalvik.system.NativeStart.main(Native Method)
    02-28 03:14:11.562: E/AndroidRuntime(10765): Caused by: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.android.gms.internal.dm.x(Unknown Source)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.enterproid.file.activity.LoginActivity.printall(LoginActivity.java:117)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at com.enterproid.file.activity.LoginActivity.onActivityResult(LoginActivity.java:74)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.app.Activity.dispatchActivityResult(Activity.java:5192)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3158)
    02-28 03:14:11.562: E/AndroidRuntime(10765):    ... 11 more

what should i do ?

  • http://stackoverflow.com/questions/17547019/calling-this-from-your-main-thread-can-lead-to-deadlock-and-or-anrs-while-getti – Nemo Feb 27 '14 at 19:53

1 Answers1

0

It won't allow you to do this on the main thread because it would lock your app until it's finished. And if this procedure takes more than 5 seconds, the Android OS would close your app.

You need to use a background thread like AsyncTasks.

HTH

Dpedrinha
  • 3,741
  • 3
  • 38
  • 57