Failed to fetch items


#1

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?

[code]package com.bignerdranch.android.photogallery;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

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

import android.net.Uri;
import android.util.Log;

public class FlickrFetchr {
public static final String TAG = “FlickrFetchr”;
private static final String ENDPOINT = “http://api.flickr.com/services/rest/”;
private static final String API_KEY = “15dcd059c0ba2a7b33c0dad088f1fd52”;
private static final String METHOD_GET_RECENT = “flickr.photos.getRecent”;
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();
	
	try{
		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 = in.read(buffer)) > 0)
			out.write(buffer, 0, bytesRead);
		out.close();
		return out.toByteArray();
	} finally{
		connection.disconnect();
	}
}

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

public ArrayList<GalleryItem> fetchItems(){
	ArrayList<GalleryItem> items = new ArrayList<GalleryItem>();
	try{
		String url = Uri.parse(ENDPOINT).buildUpon().appendQueryParameter("method", METHOD_GET_RECENT)
				.appendQueryParameter("api_key", API_KEY).appendQueryParameter(PARAM_EXTRAS, EXTRA_SMALL_URL)
				.build().toString();
		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,
IOException{
	int eventType = parser.next();
	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();
			item.setId(id);
			item.setCaption(caption);
			item.setUrl(smallUrl);
			items.add(item);
		}
		eventType = parser.next();
	}
	
	
}

}
[/code]

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


#2

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.


#3

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): java.io.FileNotFoundException: api.flickr.com/services/rest/?me … tras=url_s
05-08 14:06:35.142: E/PhotoFetcher(28901): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
05-08 14:06:35.142: E/PhotoFetcher(28901): at com.bignerdranch.android.photogallery.FlickrFetchr.getUrlBytes(FlickrFetchr.java:36)
05-08 14:06:35.142: E/PhotoFetcher(28901): at com.bignerdranch.android.photogallery.FlickrFetchr.getUrl(FlickrFetchr.java:55)
05-08 14:06:35.142: E/PhotoFetcher(28901): at com.bignerdranch.android.photogallery.FlickrFetchr.fetchItems(FlickrFetchr.java:67)
05-08 14:06:35.142: E/PhotoFetcher(28901): at com.bignerdranch.android.photogallery.PhotoGalleryFragment$FetchItemsTask.doInBackground(PhotoGalleryFragment.java:52)
05-08 14:06:35.142: E/PhotoFetcher(28901): at com.bignerdranch.android.photogallery.PhotoGalleryFragment$FetchItemsTask.doInBackground(PhotoGalleryFragment.java:1)
05-08 14:06:35.142: E/PhotoFetcher(28901): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-08 14:06:35.142: E/PhotoFetcher(28901): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-08 14:06:35.142: E/PhotoFetcher(28901): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-08 14:06:35.142: E/PhotoFetcher(28901): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-08 14:06:35.142: E/PhotoFetcher(28901): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-08 14:06:35.142: E/PhotoFetcher(28901): at java.lang.Thread.run(Thread.java:841)[/quote]


#4

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


#5

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


#6

Thanks!!!


#7

See this
http://forums.bignerdranch.com/viewtopic.php?f=423&t=8944


#8

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

In FlickrFetchr.java the correct line of code is
private static final String ENDPOINT = “https://api.flickr.com/services/rest/”;


#9

Have chinese guys fix it?


#10

This seems to be right! Thanks a ton!


#11

Thank a lot chair u really helped


#12

Using fqroute2 on the real device.


#13

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:


#14

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.


#15

thanks…it works…