package com.livefyre.android.test;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author zjj
 */
public class GETJSON {
    public static JSONObject fetchData(String endpoint) throws IOException, JSONException {
        return fetchData(new URL(endpoint));
    }

    /**
     * Performs a GET request for the given endpoint and returns a JSON object derived from the results.
     *
     * @param endpoint An HTTP endpoint to request.
     * @return A JSONObject with the results.
     * @throws IOException
     * @throws JSONException
     */
    public static JSONObject fetchData(URL endpoint) throws IOException, JSONException {
        HttpURLConnection urlConnection = null;
        JSONObject data = null;
        try {
            urlConnection = (HttpURLConnection) endpoint.openConnection();
        } catch (IOException e) {
            System.err.println("Caught IOException in com.livefyre.core.core.GETJSON.fetchData: " + e.getStackTrace());
            throw e;
        }
        try {
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            StringBuilder jsonBuilder = new StringBuilder();
            int inChar;
            while ((inChar = reader.read()) != -1) {
                char character = (char) inChar;
                jsonBuilder.append(character);
            }
            in.close();
            data = parseJSONData(jsonBuilder.toString());
        } catch (IOException e) {
            System.err.println("Caught IOException in com.livefyre.core.core.GETJSON.fetchData: " + e.getStackTrace());
            throw e;
        } finally {
            urlConnection.disconnect();
        }
        return data;
    }

    /**
     * Parses JSON string into JSON objects.
     *
     * @param json String representation of a JSON Object.
     * @return A JSON object.
     * @throws JSONException
     */
    public static JSONObject parseJSONData(String json) throws JSONException {
        try {
            return new JSONObject(json);
        } catch (JSONException e) {
            System.err.println("Caught JSONException in com.livefyre.core.core.GETJSON.fetchData: " + e.getStackTrace());
            throw e;
        }
    }
}

@@ -349,16 +349,235 @@
 TJSON %7B%0A
+    /**%0A     * Wrapper around fetchData(URL endpoint)%0A     *%0A     * @param endpoint a string representing URL.%0A     * @return A JSONObject with the result%0A     * @throws IOException%0A     * @throws JSONException%0A     */%0A
     publ
@@ -1163,34 +1163,16 @@
 = null;%0A
-        try %7B%0A    
         
@@ -1238,193 +1238,8 @@
 ();%0A
-        %7D catch (IOException e) %7B%0A            System.err.println(%22Caught IOException in com.livefyre.core.core.GETJSON.fetchData: %22 + e.getStackTrace());%0A            throw e;%0A        %7D%0A
     
@@ -1716,29 +1716,30 @@
  data = 
-parseJSONData
+new JSONObject
 (jsonBui
@@ -1770,779 +1770,88 @@
   %7D 
-catch (IOException e) %7B%0A            System.err.println(%22Caught IOException in com.livefyre.core.core.GETJSON.fetchData: %22 + e.getStackTrace());%0A            throw e;%0A        %7D finally %7B%0A            urlConnection.disconnect();%0A        %7D%0A        return data;%0A    %7D%0A%0A    /**%0A     * Parses JSON string into JSON objects.%0A     *%0A     * @param json String representation of a JSON Object.%0A     * @return A JSON object.%0A     * @throws JSONException%0A     */%0A    public static JSONObject parseJSONData(String json) throws JSONException %7B%0A        try %7B%0A            return new JSONObject(json);%0A        %7D catch (JSONException e) %7B%0A            System.err.println(%22Caught JSONException in com.livefyre.core.core.GETJSON.fetchData: %22 + e.getStackTrace());%0A            throw e;%0A        %7D
+finally %7B%0A            urlConnection.disconnect();%0A        %7D%0A        return data;
 %0A   

Simplify GETJSON class