content: package io.github.akueisara.newsapp;
  
  import android.text.TextUtils;
  import android.util.Log;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.nio.charset.Charset;
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Created by akueisara on 10/9/2016.
   */
  
  public class QueryUtils {
      public static final String LOG_TAG = QueryUtils.class.getSimpleName();
  
      private QueryUtils() {
      }
  
      public static ArrayList<News> extractResultFromJson(String newsJSON) {
          // If the JSON string is empty or null, then return early.
          if (TextUtils.isEmpty(newsJSON)) {
              return null;
          }
  
          ArrayList<News> newses = new ArrayList<>();
  
          try {
              JSONObject reader = new JSONObject(newsJSON);
              JSONObject response = reader.getJSONObject("response");
              JSONArray resultArray = response.getJSONArray("results");
              for(int i=0; i< resultArray.length(); i++) {
                  JSONObject news = resultArray.getJSONObject(i);
                  String title = news.getString("webTitle");
                  String type = news.getString("type");
                  String date = news.getString("webPublicationDate");
                  String section = news.getString("sectionName");
                  String url =  news.getString("webUrl");
                  News n = new News(title, type, date, section, url);
                  newses.add(n);
              }
          } catch (JSONException e) {
              Log.e("QueryUtils", "Problem parsing the news JSON results", e);
          }
  
          return newses;
      }
  
  
      public static List<News> fetchNewsData(String requestUrl) {
          try {
              Thread.sleep(2000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
          // Create URL object
          URL url = createUrl(requestUrl);
  
          // Perform HTTP request to the URL and receive a JSON response back
          String jsonResponse = null;
          try {
              jsonResponse = makeHttpRequest(url);
          } catch (IOException e) {
              Log.e(LOG_TAG, "Error closing input stream", e);
          }
  
          // Extract relevant fields from the JSON response and create an {@link Event} object
          List<News> news = extractResultFromJson(jsonResponse);
  
          // Return the {@link Event}
          return news;
      }
  
      private static URL createUrl(String stringUrl) {
          URL url = null;
          try {
              url = new URL(stringUrl);
          } catch (MalformedURLException e) {
              Log.e(LOG_TAG, "Error with creating URL ", e);
          }
          return url;
      }
  
      private static String makeHttpRequest(URL url) throws IOException {
          String jsonResponse = "";
  
          // If the URL is null, then return early.
          if (url == null) {
              return jsonResponse;
          }
  
          HttpURLConnection urlConnection = null;
          InputStream inputStream = null;
          try {
              urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setReadTimeout(10000 /* milliseconds */);
              urlConnection.setConnectTimeout(15000 /* milliseconds */);
              urlConnection.setRequestMethod("GET");
              urlConnection.connect();
  
              // If the request was successful (response code 200),
              // then read the input stream and parse the response.
              if (urlConnection.getResponseCode() == 200) {
                  inputStream = urlConnection.getInputStream();
                  jsonResponse = readFromStream(inputStream);
              } else {
                  Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
              }
          } catch (IOException e) {
              Log.e(LOG_TAG, "Problem retrieving the news JSON results.", e);
          } finally {
              if (urlConnection != null) {
                  urlConnection.disconnect();
              }
              if (inputStream != null) {
                  inputStream.close();
              }
          }
          return jsonResponse;
      }
  
      private static String readFromStream(InputStream inputStream) throws IOException {
          StringBuilder output = new StringBuilder();
          if (inputStream != null) {
              InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
              BufferedReader reader = new BufferedReader(inputStreamReader);
              String line = reader.readLine();
              while (line != null) {
                  output.append(line);
                  line = reader.readLine();
              }
          }
          return output.toString();
      }
  
  }
  