content: /*
   * Copyright - Adrian Rodriguez Bazaga (GitHub: AdrianBZG)
   * Licensed under Apache 2.0
   */
  
  package randomfactory;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONObject;
  
  @SuppressWarnings("deprecation")
  public class HTTPRequestFactory {
    public static JSONObject performRequestInBackground(String... params) {
  
      HttpClient client = new DefaultHttpClient();
      HttpGet request = new HttpGet(params[0]);
      HttpResponse response;
      String result = null;
      try {
        response = client.execute(request);         
        HttpEntity entity = response.getEntity();
  
        if (entity != null) {
          InputStream instream = entity.getContent();
          result = convertStreamToString(instream);
          instream.close();
  
          return new JSONObject(result);
        }
  
      } catch (ClientProtocolException e1) {
        e1.printStackTrace();
      } catch (IOException e1) {
        e1.printStackTrace();
      }
      return null;
    }
  
    private static String convertStreamToString(InputStream is) {
  
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb = new StringBuilder();
  
      String line = null;
      try {
        while ((line = reader.readLine()) != null) {
          sb.append(line + "\n");
        }
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return sb.toString();
    }
  }
  