Google Drive Rest API on Android

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 14:03:49

问题


I'm developing an app for a company and I need to integrate it with Google Drive. I can't use the native API because the company has files not created by the application that needs to be handled, I need the full drive scope, so the REST API is what I must use.

Here's the problem, the tutorials are not basic enough for me to get started since I only have a very basic understanding of JSON and REST.

The tutorial: https://developers.google.com/drive/web/integrate-create

As I understand it I need to create JSON in my Java code and then pass that through the example code?

JSON

{
  "action":"create",
  "folderId":"0ADK06pfg",
  "userId":"103354693083460731603"
}

JAVA

public class State {
  /**
   * Action intended by the state.
   */
  public String action;

  /**
   * IDs of files on which to take action.
   */
  public Collection<String> ids;

  /**
   * Parent ID related to the given action.
   */
  public String parentId;

  /**
   * Empty constructor required by Gson.
   */
  public State() {}

  /**
   * Create a new State given its JSON representation.
   *
   * @param json Serialized representation of a State.
   */

  public State(String json) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    State other = gson.fromJson(json, State.class);
    this.action = other.action;
    this.ids = other.ids;
    this.parentId = other.parentId;
  }
}

The problem is that I have no idea how to create JSON nor do I quite understand how to use the JSON when created to do things like create files and query for files.

If someone can get me as far as creating an empty file in a users root folder then I can probably take it from there, but I could really use a nudge in the right direction!


回答1:


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



来源:https://stackoverflow.com/questions/33968921/google-drive-rest-api-on-android

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