Exception connectiong to Flickr API


#1

Update: This problem only occurs when using emulator when I test it on Samsung it works fine. Got rattled forgot to test it on real device.

Hi guys, I followed step by step and in this chapter I encountered an exception which is: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

The problem occurred in FlickrFetchr. I am an Android beginner and really want to learn all the chapters of the book. This question has spent me more than 24 hours and I couldn’t sleep if I cannot resolve it. OCD thing I guess :smiley:

Really appreciate any help, thanks in advance!!!

here is the FlickrFetchr file:

public class FlickrFetchr {

private static final String TAG = "FlickrFetchr";

private static final String API_KEY = "075a834f9703a704c763d335f";

public 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) {
            throw new IOException(connection.getResponseMessage() +
                    ": with " +
                    urlSpec);
        }
        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 getUrlString(String urlSpec) throws IOException {
    return new String(getUrlBytes(urlSpec));
}

public List<GalleryItem> fetchItems() {

    List<GalleryItem> items = new ArrayList<>();

    try {
        String url = Uri.parse("https://api.flickr.com/services/rest/")
                .buildUpon()
                .appendQueryParameter("method", "flickr.photos.getRecent")
                .appendQueryParameter("api_key", API_KEY)
                .appendQueryParameter("format", "json")
                .appendQueryParameter("nojsoncallback", "1")
                .appendQueryParameter("extras", "url_s")
                .build().toString();
        String jsonString = getUrlString(url);
        Log.i(TAG, "Received JSON: " + jsonString);
        JSONObject jsonBody = new JSONObject(jsonString);
        parseItems(items, jsonBody);
    } catch (IOException ioe) {
        Log.e(TAG, "Failed to fetch items", ioe);
    } catch (JSONException je) {
        Log.e(TAG, "Failed to parse JSON", je);
    }

    return items;
}

private void parseItems(List<GalleryItem> items, JSONObject jsonBody)
        throws IOException, JSONException {

    JSONObject photosJsonObject = jsonBody.getJSONObject("photos");
    JSONArray photoJsonArray = photosJsonObject.getJSONArray("photo");

    for (int i = 0; i < photoJsonArray.length(); i++) {
        JSONObject photoJsonObject = photoJsonArray.getJSONObject(i);

        GalleryItem item = new GalleryItem();
        item.setId(photoJsonObject.getString("id"));
        item.setCaption(photoJsonObject.getString("title"));
        
        if (!photoJsonObject.has("url_s")) {
            continue;
        }
        
        item.setUrl(photoJsonObject.getString("url_s"));
        items.add(item);
    }
}

}


#2

Glad you got it working on your device.

If you want to figure out the issue on your emulator, I would start by opening up the browser app on the emulator and just seeing if any network requests work (can you get to any webpage?). Since your code works on your phone, I bet the issue isn’t with your code, but with the emulator.


#3

[quote=“cstewart”]Glad you got it working on your device.

If you want to figure out the issue on your emulator, I would start by opening up the browser app on the emulator and just seeing if any network requests work (can you get to any webpage?). Since your code works on your phone, I bet the issue isn’t with your code, but with the emulator.[/quote]

Hi cstewart, thank you for your reply! I tried using browser in emulator and it works fine, the exception shows when connecting to flickr in Photogallery only.


#4

Interesting. I’m not sure what the issue is. I’m surprised it works on a device but not on the emulator.

Are you sure the emulator isn’t running an old version of the app or something? I would also log out the url and make sure that you can hit it in a browser.

After that, maybe you could break out the debugger and get some more detail info about the exception.


#5

[quote=“cstewart”]Interesting. I’m not sure what the issue is. I’m surprised it works on a device but not on the emulator.

Are you sure the emulator isn’t running an old version of the app or something? I would also log out the url and make sure that you can hit it in a browser.

After that, maybe you could break out the debugger and get some more detail info about the exception.[/quote]

I tried to uninstall the app, new emulator and everything I can think of, but he problem just occurs on emulator. The debugger is a very nice suggestion!I have been writing programs for almost 8 years but I always like to know everything and refuse to use debugger , many people told me debugger should be my best friend :smiley: I will try on weekends. I will post more if I find out more. Thanks so much!


#6

The Flickr is using https now! May be we should use HttpsURLConnection(not HttpURLConnection). But I don’t know how to use HttpsURLConnection.


#7

Just modify connect “http://” to “https://” and enter api_key, try again.


#8

This is not working. I assume you’ve to set your ssl cert?


#9

BTW, does the book author has any patch for this problem?


#10

not working for me too… I followed the steps:

  1. Export the CA of flicker from chrome
  2. drop it in the raw/ folder
  3. use the certificate and create a SSLSockerFactory and pass it to the Https connection.

*But this is not always working :frowning: dunno what I am mising…