package com.example.geo_api05;

import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity 
{
	
	private static final String TAG_RESULTS = "results";
    private static final String TAG_GEOMETRY = "geometry";
    private static final String TAG_VIEWPORT = "viewport";
    private static final String TAG_NORTHEAST = "northeast";
    private static final String TAG_LAT = "lat";
    private static final String TAG_LNG = "lng";
    
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    
    // contacts JSONArray
    JSONArray results = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
	

        String address = "Rajkot, Gujarat, India";
        address = address.replaceAll(" ", "%20");
        
        String url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=true";

        // getting JSON string from URL
        JSONObject json = getJSONFromUrl(url);
/*
        try {
            // Getting Array of results
            results = json.getJSONArray(TAG_RESULTS);

            Toast.makeText(getApplication(),
                    "Number of results : " + results.length(),
                    Toast.LENGTH_LONG).show();

            for (int i = 0; i < results.length(); i++) {
                JSONObject r = results.getJSONObject(i);

                // geometry and location is again JSON Object
                JSONObject geometry = r.getJSONObject(TAG_GEOMETRY);

                JSONObject viewport = geometry.getJSONObject(TAG_VIEWPORT);

                JSONObject northest = viewport.getJSONObject(TAG_NORTHEAST);

                String lat = northest.getString(TAG_LAT);
                String lng = northest.getString(TAG_LNG);

                //tvlat.setText(lat);
                //tvlng.setText(lng);
            }

        } 
        catch (JSONException e) 
        {
            e.printStackTrace();
        }
		
*/		
	}

	
	public JSONObject getJSONFromUrl(String url) 
	{

	    // Making HTTP request
	    try {
	        // defaultHttpClient
	        DefaultHttpClient httpClient = new DefaultHttpClient();
	        HttpPost httpPost = new HttpPost(url);

	        HttpResponse httpResponse = httpClient.execute(httpPost);
	        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, "iso-8859-1"), 8);
	        StringBuilder sb = new StringBuilder();
	        String line = null;
	        while ((line = reader.readLine()) != null)
	        {
	            sb.append(line + "\n");
	        }
	        
	        is.close();
	        json = sb.toString();
	    } 
	    catch (Exception e) 
	    {
	        Log.e("Buffer Error", "Error converting result " + e.toString());
	    }

	    // try parse the string to a JSON object
	    try 
	    {
	        jObj = new JSONObject(json);
	    } 
	    catch (JSONException e) 
	    {
	        Log.e("JSON Parser", "Error parsing data " + e.toString());
	    }

	    // return JSON String
	    return jObj;

	}


	
}

/*
class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }
}
*/
