我正在尝试编写一个应用程序,让您搜索关键字并显示YouTube视频。我正在使用带有ImageViews的GridLayout来显示缩略图,虽然应用程序是“正在工作”没有真正发生..实际上,我发现代码不会到达某一行。 这是代码:
package com.youtubeplayer.test;
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.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;
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.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 searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems(); <<Not reaching to this line
if(searchResultList!=null){
displayThumbnails(searchResultList.iterator(),query);
}
}catch (IOException io){io.printStackTrace();}
}
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,因为应用程序没有崩溃所以没有显示..