使用android api进行Youtube搜索

时间:2014-08-29 20:59:19

标签: java android youtube-api android-youtube-api youtube-data-api

我正在尝试创建一个小应用,它将在youtube中搜索关键字并返回视频列表。然后我将使用该数据填充列表视图。我正在尝试使用youtube data api v3并使用此示例https://developers.google.com/youtube/v3/code_samples/java#search_by_keyword。但是我收到了这个错误。并且list始终返回null。知道为什么吗?

08-29 13:55:31.036: E/dalvikvm(32294): Could not find class 'com.google.api.services.youtube.YouTube$Builder', referenced from method com.example.youtubetomusic.YouTubeSearch.mainSearch
08-29 13:55:31.036: W/dalvikvm(32294): VFY: unable to resolve new-instance 2357 (Lcom/google/api/services/youtube/YouTube$Builder;) in Lcom/example/youtubetomusic/YouTubeSearch;
08-29 13:55:31.036: D/dalvikvm(32294): VFY: replacing opcode 0x22 at 0x0000
08-29 13:55:31.036: D/dalvikvm(32294): DexOpt: unable to opt direct call 0x57c4 at 0x0b in Lcom/example/youtubetomusic/YouTubeSearch;.mainSearch
08-29 13:55:31.076: I/Adreno-EGL(32294): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
08-29 13:55:31.096: D/OpenGLRenderer(32294): Enabling debug mode 0
08-29 13:55:38.146: W/System.err(32294): java.lang.NoClassDefFoundError: com.google.api.services.youtube.YouTube$Builder
08-29 13:55:38.166: D/dalvikvm(32294): GC_FOR_ALLOC freed 231K, 2% free 16978K/17240K, paused 13ms, total 13ms
08-29 13:55:38.166: W/System.err(32294):    at com.example.youtubetomusic.YouTubeSearch.mainSearch(YouTubeSearch.java:38)
08-29 13:55:38.166: W/System.err(32294):    at com.example.youtubetomusic.YoutubeMain$1.onClick(YoutubeMain.java:31)
08-29 13:55:38.166: W/System.err(32294):    at android.view.View.performClick(View.java:4438)
08-29 13:55:38.166: W/System.err(32294):    at android.view.View$PerformClick.run(View.java:18422)
08-29 13:55:38.166: W/System.err(32294):    at android.os.Handler.handleCallback(Handler.java:733)
08-29 13:55:38.166: W/System.err(32294):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-29 13:55:38.166: W/System.err(32294):    at android.os.Looper.loop(Looper.java:136)
08-29 13:55:38.166: W/System.err(32294):    at android.app.ActivityThread.main(ActivityThread.java:5001)
08-29 13:55:38.166: W/System.err(32294):    at java.lang.reflect.Method.invokeNative(Native Method)
08-29 13:55:38.166: W/System.err(32294):    at java.lang.reflect.Method.invoke(Method.java:515)
08-29 13:55:38.166: W/System.err(32294):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-29 13:55:38.166: W/System.err(32294):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-29 13:55:38.166: W/System.err(32294):    at dalvik.system.NativeStart.main(Native Method)
08-29 13:55:38.166: I/System.out(32294): null

我的代码看起来像这样

package com.example.youtubetomusic;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequest;
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.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.SearchListResponse;
import com.google.api.services.youtube.model.SearchResult;

public class YouTubeSearch {

  private static final String PROPERTIES_FILENAME = "youtube.properties";
  private  final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
  private  final JsonFactory JSON_FACTORY = new JacksonFactory();
  private  final long NUMBER_OF_VIDEOS_RETURNED = 10;
  private  YouTube youtube;
  private String nextToken;

  public  List<SearchResult> mainSearch(String userEnteredText) {
    // Read the developer key from youtube.properties
    Properties properties = new Properties();
    try {
      InputStream in = YouTubeSearch.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
      properties.load(in);

    } catch (IOException e) {
      System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause()
          + " : " + e.getMessage());
      System.exit(1);
    }

    try {
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("youtube-cmdline-search-sample").build();


      // Get query term from user.
      String queryTerm = userEnteredText;/*getInputQuery();*/

      YouTube.Search.List search = youtube.search().list("id,snippet");

      String apiKey = properties.getProperty("youtube.apikey");
      search.setKey(apiKey);
      search.setQ(queryTerm);
      /*
       * We are only searching for videos (not playlists or channels). If we were searching for
       * more, we would add them as a string like this: "video,playlist,channel".
       */
      search.setType("video");
      /*
       * This method reduces the info returned to only the fields we need and makes calls more
       * efficient.
       */
      search.setFields("items(id/kind,id/videoId,snippet/title,snippet/publishedAt,snippet/thumbnails/default/url),nextPageToken");
      search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
      SearchListResponse searchResponse = search.execute();

      List<SearchResult> searchResultList = searchResponse.getItems();
      nextToken=searchResponse.getNextPageToken();
      if (searchResultList != null) {
        return (searchResultList);
      }
    } catch (GoogleJsonResponseException e) {
      System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
          + e.getDetails().getMessage());
    } catch (IOException e) {
      System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
    } catch (Throwable t) {
      t.printStackTrace();
    }
    return null;
  }
}

1 个答案:

答案 0 :(得分:0)

我想我发现了这个问题。我使用了错误的运输工具。我将Auth.HTTP_TRANSPORT更改为HTTP_TRANSPORT,然后它可以正常工作。