android - Android 上的 Google Drive Rest API

标签 android json rest google-drive-api google-drive-android-api

我正在为一家公司开发一款应用,我需要将它与 Google 云端硬盘集成。我不能使用 native API,因为公司有文件不是由需要处理的应用程序创建的,我需要完整的驱动范围,所以我必须使用 REST API。

这就是问题所在,因为我对 JSON 和 REST 只有非常基本的了解,所以这些教程对我来说不够基础。

教程:https://developers.google.com/drive/web/integrate-create

据我了解,我需要在我的 Java 代码中创建 JSON,然后通过示例代码传递它?

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 来执行创建文件和查询文件等操作。

如果有人能让我在用户根文件夹中创建一个空文件,那么我可能可以从那里开始,但我真的可以在正确的方向上使用微调!

最佳答案

假设您正在谈论 Android 应用程序,则无需创建 JSON。 Java REST Api 在 Android 上非常容易使用。如果官方文档和例子还不够,你可以看看这个 demo .它需要更复杂一点(为了保持与 GDAA version 的兼容性),但通过几个简单的步骤,您可以简化它。

这里我当然不能复制所有的代码,但是你可以直接抢REST class ,向 setSelectedAccountName() 提供一个 GooDrive 帐户(或省略该方法并让服务处理它)并简化 connect()/disconnect() 方法。 connect() 方法(为了与 GDAA 兼容)应该被替换为这样的 try/catch 结构:

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 实现一段时间了,但从未遇到过问题。

关于 REST Api 的一般说明。由于它“依赖于网络状态”,我建议将它与应用程序的 UI 完全断开连接,并在存在 Activity 网络(WIFI、蜂窝)流量时调用的某种类型的同步服务中运行它。看网络相关剧集here .

祝你好运

关于android - Android 上的 Google Drive Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968921/

相关文章:

android - 可以将首选项屏幕添加到“设置”应用程序吗?

php - base64_decode 不适用于 PHP

xml - Web api 应该默认返回 JSon

JSON结构输出

java - .tmp 文件未在多部分 Spring MVC 文件上传中删除

rest - SunCertPathBuilderException : unable to find valid certification path to requested target in CN1 app

java - Spring Boot - 多部分 - 不支持的媒体类型

web-services - 按国家/地区获取利率

android - 如何在不自动播放过渡的情况下恢复 MotionLayout 的过渡状态?

android - android 如何检查应用程序是否退出或关闭?