本文介绍了在谷歌Android驱动器REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个公司的应用程序,我需要将它与谷歌驱动器集成。我无法使用原生API,因为该公司有没有需要处理,我需要完整的驱动器范围,应用程序创建的文件,这样的REST API是我必须用的东西。

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.

这里的问题,教程是不够的基本对我来说上手,因为我只有JSON和REST的一个非常基本的了解。

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.

本教程:

据我了解,我需要在我的Java code创建JSON,然后传递通过例如code?

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

问题是,我不知道如何创建JSON我也不大明白创建做的事情一样创建文件和查询文件时如何使用JSON。

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!

推荐答案

假设,你正在谈论一个Android应用程序,也无需创建JSON。在Java API是很容易在Android上使用。如果官方文档和例子并不足够,你可以看看这个。它是一个比较复杂,需要(为了保持与GDAA 兼容性),但几个简单的步骤,您可以简化它。

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.

我当然不能在这里复制的所有code,但你可以抢夺 REST类,提供一个GooDrive帐户setSelectedAccountName()(或省略方法,让服务处理它),并简化了的连接()/断开()的方法。应)所取代的connect()方法(与GDAA兼容)由一个try / catch结构是这样的:

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()' ???
}

任何地方,你发现在REST类中的...的execute()方法,为了赶授权的突然丧失,等等...(随时都可能发生)。否则,我已经运行了一段时间,从来没有经历过这样的问题CRUD实现。

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.

有关REST API的一个普遍注。既然是网络依赖于状态',我会建议从您的应用程序的UI完全断开,并在某种类型的同步服务运行时,有一个活动的网络(WIFI,蜂窝)通信调用。请参阅相关的网络事件。

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.

好运

这篇关于在谷歌Android驱动器REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 01:15