Drive API API Error ()

I am implementing the Google Drive APIs in an Android application. I do not want to use PendingResult # setResultCallback (), rather, I want to use PendingResult # await (), so I can get the result on the next line and not wait. This is not a problem because everything is running in the background.

However, every time I call wait (), I get the following error. I tried to make one call to the Google Drive API that uses await () in AsyncTask, Thread, IntentService, a service with AsyncTask, and a service with a thread. I got the following error every time. I tried calling Looper.prepare () on Thread implementations, but didn't solve anything.

Has anyone else encountered this problem?

java.lang.IllegalStateException: await must not be called on the UI thread at com.google.android.gms.internal.hm.a(Unknown Source) at com.google.android.gms.common.api.a$a.await(Unknown Source) at [OMITTED].GoogleDriveManager$2.onConnected(GoogleDriveManager.java:83) at com.google.android.gms.internal.hc.c(Unknown Source) at com.google.android.gms.common.api.c.eK(Unknown Source) at com.google.android.gms.common.api.cd(Unknown Source) at com.google.android.gms.common.api.c$2.onConnected(Unknown Source) at com.google.android.gms.internal.hc.c(Unknown Source) at com.google.android.gms.internal.hc.cp(Unknown Source) at com.google.android.gms.internal.hb$hb(Unknown Source) at com.google.android.gms.internal.hb$hd(Unknown Source) at com.google.android.gms.internal.hb$b.fv(Unknown Source) at com.google.android.gms.internal.hb$a.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5146) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) at dalvik.system.NativeStart.main(Native Method) 

This is the code I'm using to test the API:

  apiClient = new GoogleApiClient.Builder(mContext) .addApi(Drive.API) .addScope(Drive.SCOPE_APPFOLDER) .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle bundle) { LogUtil.log(TAG, "onConnected()"); connected = true; DriveApi.ContentsResult result = Drive.DriveApi.newContents(apiClient).await(); } @Override public void onConnectionSuspended(int i) { LogUtil.log(TAG, "onConnectionSuspended()"); connected = false; } }) .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { @Override public void onConnectionFailed(ConnectionResult result) { LogUtil.log(TAG, "onConnectionFailed()"); connected = false; } }) .build(); apiClient.connect(); 
+5
source share
1 answer

apiClient.connect() is an asynchronous operation, so calling it in AsyncTask does not help (starting a connection in the background thread does not help when onConnected() is called in the user interface thread).

Instead, you can run AsyncTask from your onConnected() callback, which AsyncTask doInBackground() could then call Drive.DriveApi.newContents(apiClient).await() .

Of course, there is no need to ever call await() - use setResultCallback () to asynchronously wait for newContents () to complete:

 @Override public void onConnected(Bundle bundle) { LogUtil.log(TAG, "onConnected()"); connected = true; Drive.DriveApi.newContents(apiClient).setResultCallback( new ResultCallback<DriveApi.ContentsResult>() { @Override public void onResult(DriveApi.ContentsResult result) { // Use result } }); } 
+6
source

Source: https://habr.com/ru/post/1204433/


All Articles