1

我在我的类路径中使用了以下 jar 来使用 YouTube API 检索我的订阅列表:

gdata-client-1.0.jar gdata-core-1.0.jar gdata-media-1.0.jar gdata-youtube-2.0.jar guava-14.0-rc1.jar mail.jar

代码如下:

import com.google.gdata.client.*;
import com.google.gdata.client.youtube.*;
import com.google.gdata.data.*;
import com.google.gdata.data.geo.impl.*;
import com.google.gdata.data.media.*;
import com.google.gdata.data.media.mediarss.*;
import com.google.gdata.data.youtube.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class YouTubeExample {


    public static void main (String args[]) throws MalformedURLException, IOException, ServiceException{
        String developer_key = "CSCSCSCSCSCScSCXXXXX-XXXXX_-XXXX";
        YouTubeService service = new YouTubeService(developer_key); 

        //Retrieving video subscriptions
        String feedUrl =
                "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/subscriptions";

        SubscriptionFeed feed = service.getFeed(new URL(feedUrl), SubscriptionFeed.class);

        for(SubscriptionEntry entry : feed.getEntries()) {
            System.out.println("Title: " + entry.getTitle().getPlainText());
            System.out.println("Feed Link: " + entry.getFeedUrl());
        }

    }

}

但是在运行它时,我收到如下错误:

线程“主”java.lang.NoSuchMethodError 中的异常:com.google.common.collect.ImmutableSet.of([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet; 在 com.google.gdata.wireformats.AltFormat$Builder.setAcceptableTypes(AltFormat.java:399) 在 com.google.gdata.wireformats.AltFormat$Builder.setAcceptableXmlTypes(AltFormat.java:387) 在 com.google.gdata.wireformats .AltFormat.(AltFormat.java:49) 在 com.google.gdata.client.Service.(Service.java:535) 在 YouTubeExample.main(YouTubeExample.java:21)

如果我在类路径中用 guava-10.0.1.jar(根据一些建议的旧版本)替换 guava-14.0-rc1.jar,我仍然会收到如下错误:

线程“主”java.lang.NoSuchMethodError 中的异常:com.google.gdata.data 处的 com.google.gdata.data.ExtensionProfile.declareAdditionalNamespace(Lcom/google/gdata/util/common/xml/XmlWriter$Namespace;)V .youtube.CommentEntry.declareExtensions(CommentEntry.java:92) 在 com.google.gdata.data.ExtensionProfile.addDeclarations(ExtensionProfile.java:71) 在 com.google.gdata.data.BaseFeed.declareExtensions(BaseFeed.java:229 ) com.google.gdata.data.ExtensionProfile.addDeclarations(ExtensionProfile.java:71) com.google.gdata.client.youtube.YouTubeService.(YouTubeService.java:140) com.google.gdata.client.youtube .YouTubeService.(YouTubeService.java:103) 在 YouTubeExample.main(YouTubeExample.java:21)

我试过包括activation.jar,servlet-api.jar,但没有运气!我也尝试过旧版本,例如 guava 0.7 jar,但没有运气。请帮忙!

4

1 回答 1

2

不要全部导入。

您的项目似乎同时依赖于最近和旧的 API。你不应该。因此,只需检查此页面的内容即可。

使用 Maven 运行代码时,我只需要以下内容:

<dependencies>
    <dependency>
        <groupId>com.google.gdata</groupId>
        <artifactId>core</artifactId>
        <version>1.47.1</version>
    </dependency>
</dependencies>

Maven 很有趣,因为我们可以看到所有隐藏的依赖项,我们可以手动管理它们以正确安装所有最新版本的库。

完整的树包括以下内容:

core: 1.47.1
  guava: 13.0.1
  google-oauth-client-jetty: 1.11.0-beta
    google-oauth-client-java6: 1.11.0-beta
      google-auth-client: 1.11.0-beta
        google-http-client: 1.11.0-beta
          jsr305: 1.3.9 (omitted for conflict with 1.3.7)
          guava: 11.0.1 (omitted for conflict with 13.0.1)
          httpclient: 4.0.3
            httpcore: 4.0.1
            commons-logging: 1.1.1
            commons-codec: 1.3
          xpp3: 1.1.4
        jsr305: 1.3.9 (omitted for conflict with 1.3.7)
        guava: 11.0.1 (omitted for conflict with 13.0.1)
    jetty: 6.1.26
      jetty-util: 6.1.26
      servlet-api: 2.5-20081211
  jsr305: 1.3.7
  mail: 1.4
    activation: 1.1

这棵树向我们展示了使用 guava 13.0.1 是安全的,并且您实际上并不需要您尝试包含的所有其他 API,因此只需忽略它们即可。

如果您不使用 Maven,只需使用此处的项目:https ://code.google.com/p/gdata-java-client/downloads/list

于 2013-02-20T10:13:37.410 回答