Failed to fetch items


My code doesn’t connect to the URL properly, and through heavy debugging I’ve determined that the URL isn’t being created properly. I’ve double checked my code with the book and everything seems to be correct. Can anyone take a look and point me to the problem and how to fix it?


import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class FlickrFetchr {
public static final String TAG = “FlickrFetchr”;
private static final String ENDPOINT = “”;
private static final String API_KEY = “15dcd059c0ba2a7b33c0dad088f1fd52”;
private static final String METHOD_GET_RECENT = “”;
private static final String PARAM_EXTRAS = “extras”;
private static final String EXTRA_SMALL_URL = “url_s”;
private static final String XML_PHOTO = “photo”;

byte[] getUrlBytes(String urlSpec) throws IOException {
	URL url = new URL(urlSpec);
	HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		InputStream in = connection.getInputStream();
		if(connection.getResponseCode() != HttpURLConnection.HTTP_OK)
			return null;
		int bytesRead = 0;
		byte[] buffer = new byte[1024];
		while((bytesRead = > 0)
			out.write(buffer, 0, bytesRead);
		return out.toByteArray();
	} finally{

public String getUrl(String urlSpec) throws IOException{
	return new String(getUrlBytes(urlSpec));

public ArrayList<GalleryItem> fetchItems(){
	ArrayList<GalleryItem> items = new ArrayList<GalleryItem>();
		String url = Uri.parse(ENDPOINT).buildUpon().appendQueryParameter("method", METHOD_GET_RECENT)
				.appendQueryParameter("api_key", API_KEY).appendQueryParameter(PARAM_EXTRAS, EXTRA_SMALL_URL)
		String xmlString = getUrl(url);
		Log.i(TAG, "Received xml: " + xmlString);
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser parser = factory.newPullParser();
		parser.setInput(new StringReader(xmlString));

		parseItems(items, parser);
	catch(IOException ioe){
		Log.e(TAG, "Failed to fetch items", ioe);
	catch(XmlPullParserException xppe){
		Log.e(TAG, "Failed to parse items", xppe);
	return items;

void parseItems(ArrayList<GalleryItem> items, XmlPullParser parser) throws XmlPullParserException,
	int eventType =;
	while(eventType != XmlPullParser.END_DOCUMENT){
		if(eventType == XmlPullParser.START_TAG && XML_PHOTO.equals(parser.getName())){
			String id = parser.getAttributeValue(null, "id");
			String caption = parser.getAttributeValue(null, "title");
			String smallUrl = parser.getAttributeValue(null, EXTRA_SMALL_URL);
			GalleryItem item = new GalleryItem();
		eventType =;


I believe the problem resides within the method fetchItems(), specifically lines 56-59 when the call to getUrl(url) is made.


I have the same problem and I don’t know how to solved. :frowning:
PS: I am in China, and cannot access Flickr with http, but can access with Https.


Can someone please help with this?

I also have the same problem…

[quote]05-08 14:06:35.142: E/PhotoFetcher(28901): Failed to fetch items
05-08 14:06:35.142: E/PhotoFetcher(28901): … tras=url_s
05-08 14:06:35.142: E/PhotoFetcher(28901): at
05-08 14:06:35.142: E/PhotoFetcher(28901): at
05-08 14:06:35.142: E/PhotoFetcher(28901): at
05-08 14:06:35.142: E/PhotoFetcher(28901): at
05-08 14:06:35.142: E/PhotoFetcher(28901): at$FetchItemsTask.doInBackground(
05-08 14:06:35.142: E/PhotoFetcher(28901): at$FetchItemsTask.doInBackground(
05-08 14:06:35.142: E/PhotoFetcher(28901): at android.os.AsyncTask$
05-08 14:06:35.142: E/PhotoFetcher(28901): at
05-08 14:06:35.142: E/PhotoFetcher(28901): at android.os.AsyncTask$SerialExecutor$
05-08 14:06:35.142: E/PhotoFetcher(28901): at java.util.concurrent.ThreadPoolExecutor.runWorker(
05-08 14:06:35.142: E/PhotoFetcher(28901): at java.util.concurrent.ThreadPoolExecutor$
05-08 14:06:35.142: E/PhotoFetcher(28901): at[/quote]


Flickr recently changed their interface to require https. I had the same problem and using https fixed it.


Clarkshu! you are a genius and it was so simple too!

Thank you so much I don’t think I would have ever picked up on it lol




See this


Thank you, I had the same problem. This was very helpful.

In the correct line of code is
private static final String ENDPOINT = “”;


Have chinese guys fix it?


This seems to be right! Thanks a ton!


Thank a lot chair u really helped


Using fqroute2 on the real device.


Holy moly, this was a helpful thread. Thanks so much for making this, I was so lost after going through all the project folders and determining that I had exactly what the book had… this thread saved this project for me! :smiley:


Hello,brother! I am in China too. Since we can not access Flickr with http,how did you do in this chapter? I am confused in this chapter,and the following chapters.


thanks…it works…