package com.bignerdranch.android.photogallery;

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class FlickrFetcher {

    private static final String TAG = "FlickrFetcher";

    private static final String API_KEY = "SECRET_KEY_HERE";

    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 void fetchItems() {
        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 = null;

            jsonString = getUrlString(url);
            Log.i(TAG, "Received JSON: " + jsonString);
        } catch (IOException e) {
            Log.i(TAG, "Failed to fetch: " + e);
        }
    }
}

@@ -91,16 +91,103 @@
 l.Log;%0A%0A
+import org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 import j
@@ -327,16 +327,39 @@
 net.URL;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -2137,16 +2137,78 @@
 tring);%0A
+            JSONObject jsonBody = new JSONObject(jsonString);%0A
         
@@ -2245,25 +2245,25 @@
         Log.
-i
+e
 (TAG, %22Faile
@@ -2295,13 +2295,855 @@
    %7D
+ catch (JSONException e) %7B%0A            Log.e(TAG, %22Failed to parse JSON: %22 + e);%0A        %7D%0A    %7D%0A%0A    public void parseItems(List%3CGalleryItem%3E items, JSONObject jsonBody) throws JSONException %7B%0A%0A        JSONObject photosJSONObject = jsonBody.getJSONObject(%22photos%22);%0A        JSONArray photosJSONArray = photosJSONObject.getJSONArray(%22photo%22);%0A%0A        for (int i = 0; i %3C photosJSONArray.length(); i++) %7B%0A            JSONObject photoJSONObject = photosJSONArray.getJSONObject(i);%0A%0A            GalleryItem item = new GalleryItem();%0A            item.setId(photoJSONObject.getString(%22id%22));%0A            item.setCaption(photoJSONObject.getString(%22title%22));%0A%0A            if (!photoJSONObject.has(%22url_s%22)) %7B%0A                continue;%0A            %7D%0A            item.setUrl(photoJSONObject.getString(%22url_s%22));%0A            items.add(item);%0A        %7D
 %0A    %7D%0A%7D
 %0A
@@ -3141,9 +3141,8 @@
 %0A    %7D%0A%7D
-%0A

Create parser for GalleryItem