我正在为个人提议写一个简单的Youtube应用程序.. 在Search.execute()应用程序“正常工作”但没有做任何事情之后,代码将无法运行。 这是我的代码:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.*;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.ResourceId;
import com.google.api.services.youtube.model.SearchListResponse;
import com.google.api.services.youtube.model.SearchResult;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class fYoutube extends Fragment{
/*Global Variables*/
static final long NUMBER_OF_VIDEOS = 10; // To show only 10 videos at a time.
// static final String PROPERTIES_FILENAME = "youtube.properties";
private YouTube youTube;
ImageView imageView;
String apiKey = "AIzaSyD47jgw5kPCDUOoAvWPs-4jLFGW6ZinYrI";
public fYoutube() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_youtube, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void getQuery(String query){
if (query.length()<1) query="Youtube"; // Default search word is 'Youtube'.
try {
youTube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
}
}).setApplicationName(getString(R.string.app_name)).build();
YouTube.Search.List search = youTube.search().list("id,snippet");
search.setKey(apiKey);
search.setQ(query); //What to search for.
search.setType("video");
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEOS);
SearchListResponse searchListResponse = search.execute(); << After this line, the app won't do anything..
List<SearchResult> searchResultList = searchListResponse.getItems();
displayThumbnails(searchResultList.iterator(),query);
// if(searchResultList!=null){
// }
}catch(IOException e){}
}
public void displayThumbnails(Iterator<SearchResult> iteratorSearchResults, String query){
if(!iteratorSearchResults.hasNext()){ Toast.makeText(getActivity(),"No results for: "+query,Toast.LENGTH_LONG).show();}
while(iteratorSearchResults.hasNext()){
SearchResult singleVideo = iteratorSearchResults.next();
ResourceId resourceId = singleVideo.getId();
if(resourceId.getKind().equals("youtube#video")){
try {
String img_url = "http://img.youtube.com/vi/" + resourceId + "/default.jpg";
imageView = (ImageView) getActivity().findViewById(R.id.thumbnail1);
Picasso.with(getActivity()).load(img_url).placeholder(R.drawable.ic_launcher).into(imageView);
}catch (Exception e){e.printStackTrace();}
}
}
}
}
我不知道该怎么做.. 没有添加logcat,因为应用程序没有崩溃。
答案 0 :(得分:0)
因此,当您调用search.execute()时,您将返回SearchListResponse。之后该应用程序不执行任何操作,因为您没有告诉应用程序对返回的值执行某些操作。
您需要对该SearchListResponse执行某些操作。 所以你应该取消注释search.execute()下面的行。