Java tutorial
package org.notfunnynerd.opencoinmap.utils; /** * This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 * Unported License. To view a copy of this license, visit * http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative * Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. * * Largely inspired by Prusnak's work : https://github.com/prusnak/coinmap * Map Data CC-BY-SA by OpenStreetMap http://openstreetmap.org/ * Icons CC-0 by Brian Quinion http://www.sjjb.co.uk/mapicons/ * * @author NotFunnyNerd <notfunnynerd@gmail.com> - 2013 * */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class JSONParser { public static JSONObject getJSONfromUrl(String url) { InputStream is = null; JSONObject jObj = null; String json = ""; try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString().trim(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // Log.i("JSONParser", json); try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSONParser", "Error parsing data " + e.toString()); } return jObj; } }