获取谷歌驱动器的帐户信息的问题

时间:2014-02-27 19:34:11

标签: android google-drive-api

我正在谷歌驱动器上开发一个Android应用程序,我想获得驱动器中使用的用户名和空间,并尝试按照https://developers.google.com/drive/v2/reference/about/get这样做,我尝试了以下代码,

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

但是,它返回错误,日志cat显示

   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

我该怎么办?

1 个答案:

答案 0 :(得分:0)

它不允许你在主线程上执行此操作,因为它会锁定你的应用程序直到它完成。如果此过程需要5秒以上,Android操作系统将关闭您的应用程序。

您需要使用像AsyncTasks这样的后台线程。

HTH