【问题标题】:GWT Java how to convert List<File> to something the client side can use?GWT Java 如何将 List<File> 转换为客户端可以使用的东西?
【发布时间】:2014-11-13 02:55:17
【问题描述】:

已编辑

这可能是一个愚蠢的问题,但我无法弄清楚。我在 Eclipse 中使用 Java 和 GWT 来创建一个 RPC 应用程序来获取谷歌驱动器元数据。一切正常(我在服务器端取回元数据),但我不知道如何将这些数据传递到客户端以便我可以显示它。

我正在获取 google drive 文档的元数据列表,如下所示:

public List<File> getFromRemoteServer()
        throws HowToListingException {

List<File> lista = null;

try {
    lista = retrieveAllFiles(getDriveService("email@xxxx.org"));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (GeneralSecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return lista;

}   

我的问题是,如何将 List 类型更改为客户端可以使用的类型?即我在这里放什么(填空)?以及如何将 lista 转换为该类型?

    asyncSvc.getFromRemoteServer( {
        new AsyncCallback<____________>() {
    }

希望更多地澄清信息:

我知道要完成这项工作,我必须序列化 File 对象。我只是不确定我是否可以或将其放在哪里。 http://www.gwtproject.org/doc/latest/tutorial/RPC.html#serialize

在服务器端实现中,我使用的是这个文件:

导入 com.google.api.services.drive.model.File;

我需要使用它来让这段代码正常工作:

public static Drive getDriveService(String userEmail) throws GeneralSecurityException,
    IOException, URISyntaxException {

  HttpTransport httpTransport = new NetHttpTransport();
  JacksonFactory jsonFactory = new JacksonFactory();
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(httpTransport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
      .setServiceAccountScopes(Arrays.asList(DriveScopes.DRIVE))
      .setServiceAccountUser(userEmail)
      .setServiceAccountPrivateKeyFromP12File(
          new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
      .build();
  Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential).build();
  return service;
}

    private static List<File> retrieveAllFiles(Drive service) throws IOException {

        List<File> result = new ArrayList<File>();
        Files.List request = null;
        try {
                  request = service.files().list();
                  FileList files = request.setQ("'"+"0B9lpwZZfxxxxxxxxVeEJFR3M"+"' in parents and trashed=false").execute();
                  result.addAll(files.getItems());          
                  request.setPageToken(files.getNextPageToken());
                } 
              catch (IOException e)
              {
                System.out.println("An error occurred: " + e);
                request.setPageToken(null);
              }

        for(File f:result)
          {
              System.out.println(f.getTitle());
              System.out.println(f.getOwners());
              System.out.println(f.getModifiedDate());

          }
        return result;
      }

当我尝试使用时问题来了

import com.google.api.services.drive.model.File;

在客户端的同步和异步接口和客户端代码(入口点)。我收到有关“您是否忘记继承所需模块 - 找不到此导入的代码”的错误消息。我假设这意味着我不能在客户端使用它。我尝试导入 java.io.File ,但随后收到无法在两种类型之间转换的消息。

我觉得我已经很接近了,我只需要朝着正确的方向前进。任何帮助表示赞赏。

【问题讨论】:

    标签: java gwt google-drive-api


    【解决方案1】:

    您只能传递实现 Serializable 的类。我不知道File 类是什么,但如果它不是可序列化的,则无法使用 RPC 传递它。

    如果File 实现了Serializable,您可以将ArrayList&lt;File&gt; 传递给AsyncCallback。请注意,对于 GWT,最好使用特定的实现(ArrayList 而不是 List)以减少编译代码。

    【讨论】:

    • 好的,所以我在这方面还很陌生,并试图在我去的时候自学。我不认为我问得很好。我将编辑我的问题,希望能提供更多说明。您和 keuleJ 很可能会给我正确的答案,而我只是不知道如何实现它。感谢您的耐心等待。
    【解决方案2】:

    设计您自己的包含您需要显示的信息的类。也许是一条独特的道路。将该类发送给客户端,该类应该是可序列化的。

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 2019-12-20
      相关资源
      最近更新 更多