我试图使用YouTube Data API
。他们创建了一个Java客户端。我试图遵循的Here is the sample向他们的服务器发送请求。我不能拥有这个
// 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();
}
}
}
}
我试过
return params[0].execute();
来自doInBackground
方法,但它让我考虑了可能的IOException
。所以我改变了代码,现在它说variable 'a' might not have been initialized
。我该怎么办?
答案 0 :(得分:0)
也许如果你初始化你的变量至少是一个null
SearchListResponse a = null;
它将删除a not initialized