Google Drive Rest API on Android

倾然丶 夕夏残阳落幕 提交于 2019-12-04 19:13:32

Assuming, that you're talking about an Android app, there is no need to create JSON. The Java REST Api is quite easy to use on Android. If the official docs and the examples do not suffice, you may look at this demo. It is a bit more complex that needs to be (in order to maintain compatibility with the GDAA version), but with a few simple steps, you may simplify it.

I certainly can't copy all the code here, but you can just snatch the REST class, supply a GooDrive account to setSelectedAccountName() (or omit the method and let the service handle it) and simplify the connect() / disconnect() methods. The connect() method (for compatibility with GDAA) should be) replaced by a try/catch construct like this:

com.google.api.services.drive.Drive mGOOSvc;
...  
try {
  mGOOSvc...execute();
} catch (UserRecoverableAuthIOException uraIOEx) {  
  // standard authorization failure - user fixable
} catch (GoogleAuthIOException gaIOEx) {  
  // usually PackageName / SHA1 mismatch in DevConsole
} catch (IOException e) {   
  // '404 not found' in FILE scope, still, consider connected
  if (e instanceof GoogleJsonResponseException) {
    if (404 == ((GoogleJsonResponseException) e).getStatusCode())
      mConnected = true;
  }
} catch (Exception e) {  
  // "the name must not be empty" indicates
  // UNREGISTERED / EMPTY account in 'setSelectedAccountName()' ???
}

anywhere you find the '...execute()' method in the REST class in order to catch sudden loss of authorization, etc... (can happen anytime). Otherwise, I've been running this CRUD implementation for some time and never experienced problems.

One general note about the REST Api. Since it is 'network state dependent', I would recommend to disconnect it completely from your app's UI and run it in some type of sync service invoked when there is an active network (WIFI, cellular) traffic. See the network related episodes here.

Good Luck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!