Android Open Source - GoogleImageLoader J S O N Parser






From Project

Back to project page GoogleImageLoader.

License

The source code is released under:

GNU General Public License

If you think the Android project GoogleImageLoader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.challenge.uber.imageloader.parser;
/*  w w  w.  j a va  2  s  .c  om*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.GeneralSecurityException;

import javax.net.ssl.SSLContext;

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

import com.squareup.okhttp.OkHttpClient;

/**
 * JSON Parser for converting the data from the Google Image Request API to a JSON Object.
 * @author Julien Salvi
 */
public class JSONParser {
    
  //JSON and OkHttp client reference.
  private String json;
  private OkHttpClient client;
  
  /**
   * Default constructor with the current context.
   * @param _context Current context.
   */
  public JSONParser() {
    client = new OkHttpClient();
  }
  
  /**
   * Fast get request thanks to the OkHttp library.
   * @param url URL as a string.
   * @return The JSON object
   * @throws IOException
   */
  public JSONObject fastGetRequest(String url) throws IOException {
    SSLContext sslContext = null;
      try {
        sslContext = SSLContext.getInstance("TLS");
          sslContext.init(null, null, null);
      } catch (GeneralSecurityException e) {
        e.printStackTrace();
      }
      client.setSslSocketFactory(sslContext.getSocketFactory());
      HttpURLConnection connection = client.open(new URL(url));
      connection.setUseCaches(true);
      InputStream in = null;
      try {
        // Read the response.
        in = connection.getInputStream();
        byte[] response = readFully(in);
        json = new String(response, "UTF-8");
        return new JSONObject(json);
      } catch (IOException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    } finally {
      if (in != null) in.close();
      }
    return null;
  }
  
  /**
   * Read the input stream
   * @param in Current input stream
   * @return The output stream as a byte array.
   * @throws IOException IO exception
   */
  private byte[] readFully(InputStream in) throws IOException {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      for (int count; (count = in.read(buffer)) != -1; ) {
        out.write(buffer, 0, count);
      }
      return out.toByteArray();
  }

}




Java Source Code List

com.challenge.uber.imageloader.ImageViewerActivity.java
com.challenge.uber.imageloader.SearchImageActivity.java
com.challenge.uber.imageloader.adapter.SearchHistoryAdapter.java
com.challenge.uber.imageloader.adapter.SearchImageAdapter.java
com.challenge.uber.imageloader.fragments.ImageViewerFragment.java
com.challenge.uber.imageloader.parser.JSONParser.java
com.challenge.uber.imageloader.utils.ShadowedTransformation.java
com.challenge.uber.imageloader.utils.Utils.java