After Lots of trial i got following solution.

I am writing full solution,with the intention that everyone can take
benefit of it.and getting solution without wasting valuable time,like
me.

package com.example.Jsonparse;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class JsonparseActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try
        {
                String Lat=Double.toString(22.508507985602836);
                String Long=Double.toString(73.474991977022533);
                
                String Address= ReadAddressFromWebService(Lat,Long);
                Toast.makeText(this,Address, Toast.LENGTH_LONG).show();
        }

        catch(Exception e)
        {
                appendLog("Json parsing error :"+ e.toString());
                 Toast.makeText(this,e.toString(), Toast.LENGTH_LONG).show();
        }
        }

        public String ReadAddressFromWebService(String latitude, String 
longitude) {
                String Address="";
                 StringBuffer sb=new StringBuffer();
         sb.append("http://maps.googleapis.com/maps/api/geocode/json?latlng="+
latitude +","+longitude +"&sensor=false");
         String url=sb.toString();
         HttpClient httpClient=new DefaultHttpClient();

         appendLog("HTTP client created");
         String responseData="";
         try {
             HttpResponse response=httpClient.execute(new HttpGet(url));
             response.addHeader("Accept-Language", "en-US");
             HttpEntity entity=response.getEntity();
             appendLog("HTTP Response arrived");

             BufferedReader bf=new BufferedReader(new
InputStreamReader((entity.getContent()),"UTF-8"));
             String line="";
             appendLog("Start buffre reading");

             while((line=bf.readLine())!=null){
                 responseData=responseData+line;
             }

             JSONObject jsonObj = new JSONObject(responseData);

             JSONArray resultArry = jsonObj.getJSONArray("results");

             Address
=resultArry.getJSONObject(0).getString("formatted_address").toString();

         } catch (Exception e) {
             // TODO Auto-generated catch block
                 appendLog("Parsing Problem: "+e.toString());
         }

         return Address;
        }
        
        public void appendLog(String text)
        {
           File logFile = new File("sdcard/exception.txt");
           if (!logFile.exists())
           {
              try
              {
                 logFile.createNewFile();
              }
              catch (Exception e)
              {
                 // TODO Auto-generated catch block
                  e.toString();
              }
           }
           try
           {
              //BufferedWriter for performance, true to set append to file flag
              BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, 
true));
              buf.append(text);
              buf.newLine();
              buf.close();
           }
           catch (Exception e)
           {
              // TODO Auto-generated catch block
                   e.toString();
           }
        }
}


On 1/28/12, Dhaval Varia <[email protected]> wrote:
> Thanks for reply..
> Will get back to you after plugging the code..
> On Jan 28, 2012 10:54 AM, "Shubhangi" <[email protected]> wrote:
>
>> hello
>>
>> first of all let me tell u regarding ur track for JSON parsing and
>> getting
>> address using a web service is right..
>> now here I'm sending u code that how I am used to parse the JSON data..
>> All you have to do is check this specified URL and change the rest of the
>> code according your requirements..
>>
>> code is given below
>>
>>
>> --------------------------------------------------------------------------------------------------------------------
>>
>> ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String,
>> String>>();
>>         JSONObject json = JSONfunctions.getJSONfromURL("
>> http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
>> ");
>>         try{
>>         JSONArray  earthquakes = json.getJSONArray("earthquakes");
>>             for(int i=0;i<earthquakes.length();i++){
>>  HashMap<String, String> map = new HashMap<String, String>();
>> JSONObject e = earthquakes.getJSONObject(i);
>>  map.put("id",  String.valueOf(i));
>>          map.put("name", "Earthquake name:" + e.getString("eqid"));
>>          map.put("magnitude", "Magnitude: " +  e.getString("magnitude"));
>>          mylist.add(map);
>>  }
>>         }catch(JSONException e)        {
>>          Log.e("log_tag", "Error parsing data "+e.toString());
>>         }
>>
>>         ListAdapter adapter = new SimpleAdapter(this, mylist ,
>> R.layout.main,
>>                         new String[] { "name", "magnitude" },
>>                         new int[] { R.id.item_title, R.id.item_subtitle
>> });
>>
>>         setListAdapter(adapter);
>>
>>
>>
>>
>> -------------------------------------------------------------------------------------------------------------------------------------
>>
>> let me know whether it works or not for you...
>>
>> thank you
>>
>>
>


-- 
Thanks & Best Regards.
----------------------------------------------------------------------------------------
Dhaval varia
Assistant Professor
Govt Engineering College,Modasa
(9924343883)

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to