2016-04-07 11 views
0

私はYouTube Data APIを使用しようとしています。彼らはJavaクライアントを作成しました。 Here is the sample私は彼らのサーバーにリクエストを送信しようとしました。私はこれを持つことができないAsyncTask doInBackground()がnullを返す

// Call the API and print results. 
SearchListResponse searchResponse = search.execute(); 
List<SearchResult> searchResultList = searchResponse.getItems(); 

メインスレッドでは、Androidはメインネット上でネットワーキングを許可しないため、メインスレッドにはありません。だから私はAsyncTask

public class youtubeSearchTask extends AsyncTask<YouTube.Search.List, Void, SearchListResponse> { 

    @Override 
    protected SearchListResponse doInBackground(YouTube.Search.List... params) { 
     SearchListResponse a; 
     try { 
      a = params[0].execute(); 
     } catch (java.io.IOException ioe) { 
      ioe.printStackTrace(); 
     } 
     return a; 
    } 

    @Override 
    protected void onPostExecute(SearchListResponse searchResponse) { 
     List<SearchResult> searchResultList = searchResponse.getItems(); 
     List<String> videoIds = new ArrayList<String>(); 

     if (searchResultList != null) { 

      // Merge video IDs 
      for (SearchResult searchResult : searchResultList) { 
       videoIds.add(searchResult.getId().getVideoId()); 
      } 
      Joiner stringJoiner = Joiner.on(','); 
      String videoId = stringJoiner.join(videoIds); 

      // Call the YouTube Data API's youtube.videos.list method to 
      // retrieve the resources that represent the specified videos. 
      try { 
       YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet, statistics").setId(videoId); 
       new videoSearchTask().execute(listVideosRequest); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

を使用しようとした私はdoInBackgroundメソッド内から

return params[0].execute(); 

を試してみましたが、それは可能IOExceptionのための私のアカウントを作りました。だから私はコードを変更し、今はvariable 'a' might not have been initializedと言っています。私は何をしますか?

答えて

0

あなたの変数を初期化する場合はたぶん、少なくともヌル SearchListResponse a = null;と、それはa not initialized

を削除します
関連する問題