0

我想製作一個小應用程序,它將在YouTube上搜索關鍵字並返回視頻列表。然後,我將使用該數據填充列表視圖。我正在嘗試使用YouTube數據api v3並使用此示例https://developers.google.com/youtube/v3/code_samples/java#search_by_keyword。但我得到這個錯誤。列表總是返回null。任何想法爲什麼?Youtube使用android api搜索

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; 
    } 
} 

回答

0

我想我找到了問題。我使用了錯誤的運輸工具。我將Auth.HTTP_TRANSPORT更改爲HTTP_TRANSPORT,那麼它的工作正常。