我在google开发者网站上运行YouTubeSample。我的代码没有错误,我的导入似乎没问题。但是当我运行项目时,我得到了上述错误。
我做了一些搜索,但说实话,我一直无法解决问题所在。我已经尝试过导入一个外部jar番石榴,但它没有帮助。
感谢任何帮助。这是完整的课程
package com.pengilleys.googlesamples;
import java.io.IOException;
import java.util.List;
import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.json.JsonCParser;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.util.Key;
public class YouTubeSample {
public static class VideoFeed {
@Key List<Video> items;
}
public static class Video {
@Key String title;
@Key String description;
@Key Player player;
}
public static class Player {
@Key("default") String defaultUrl;
}
public static class YouTubeUrl extends GenericUrl {
@Key final String alt = "jsonc";
@Key String author;
@Key("max-results") Integer maxResults;
YouTubeUrl(String url) {
super(url);
}
}
public static void main(String[] args) throws IOException {
// set up the HTTP request factory
HttpTransport transport = new NetHttpTransport();
final JsonFactory jsonFactory = new JacksonFactory();
HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
// set the parser
JsonCParser parser = new JsonCParser();
parser.jsonFactory = jsonFactory;
request.addParser(parser);
// set up the Google headers
GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("Google-YouTubeSample/1.0");
headers.gdataVersion = "2";
request.headers = headers;
}
});
// build the YouTube URL
YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos");
url.author = "searchstories";
url.maxResults = 2;
// build the HTTP GET request
HttpRequest request = factory.buildGetRequest(url);
// execute the request and the parse video feed
VideoFeed feed = request.execute().parseAs(VideoFeed.class);
for (Video video : feed.items) {
System.out.println();
System.out.println("Video title: " + video.title);
System.out.println("Description: " + video.description);
System.out.println("Play URL: " + video.player.defaultUrl);
}
} }
答案 0 :(得分:7)
setup documentation给出了依赖项列表:
根据您正在构建的应用程序,您可能还需要这些依赖项:
在这种情况下,看起来它是缺少的番石榴。我不知道你对“导出”Guava的意思,但是如果你在运行代码时在类路径中包含了Guava r09 jar文件,它应该没问题。
答案 1 :(得分:2)
);
以上的额外// build the YouTube URL
是什么意思,是否意味着在该行上关闭main
?