1. String surl = "http://mobilemaps.clients.google.com/glm/mmap";
  2. HttpClient httpclient = new DefaultHttpClient();
  3. HttpPost httppost = new HttpPost(surl);
  4. httppost.setEntity(new MyCellIDRequestEntity(shortcid, lac));
  5. HttpResponse response = httpclient.execute(httppost);
  6. HttpEntity entity = response.getEntity();
  7. DataInputStream dis = new DataInputStream(entity.getContent());
  8.  
  9. // Read some prior data
  10. dis.readShort();
  11. dis.readByte();
  12. // Read the error-code
  13. int errorCode = dis.readInt();
  14. System.err.println("Error Code iss::" + errorCode);
  15. int api = myPrefs2.getInt("api", 0);
  16. if (errorCode == 0)
  17. {
  18. lat = (double) dis.readInt() / 1000000D;
  19. lng = (double) dis.readInt() / 1000000D;
  20. System.err.println("lattitude::" + Double.toString(lng) + "::long::" + Double.toString(lat));
  21. } else {
  22. // System.err.println("Wrong Error Code");
  23. }
  24. } catch (Exception e) {
  25. }
  26.  
  27. String location_name = getmylocation(latitude+","+longitude)
  28.  
  29. public String getmylocation(String value)
  30. {
  31. JSONObject json = JSONfunctions.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng="+value+"&sensor=true");
  32. try{
  33. JSONArray georesult = json.getJSONArray("results");
  34. JSONObject e = georesult.getJSONObject(0);
  35.  
  36. String result = e.getString("address_components");
  37. JSONArray jArray1 = new JSONArray(result);
  38. JSONObject json_data=jArray1.getJSONObject(3);
  39.  
  40. loc = json_data.getString("long_name");
  41.  
  42. }catch(JSONException e)
  43. {
  44. e.printStackTrace();
  45. }
  46. return loc;
  47. }
  48.  
  49. package com.sample.getcurrentlocation;
  50.  
  51. import java.io.BufferedReader;
  52. import java.io.InputStream;
  53. import java.io.InputStreamReader;
  54.  
  55. import org.apache.http.HttpEntity;
  56. import org.apache.http.HttpResponse;
  57. import org.apache.http.client.HttpClient;
  58. import org.apache.http.client.methods.HttpPost;
  59. import org.apache.http.impl.client.DefaultHttpClient;
  60. import org.json.JSONException;
  61. import org.json.JSONObject;
  62.  
  63. import android.util.Log;
  64.  
  65. public class JSONfunctions {
  66.  
  67. public static JSONObject getJSONfromURL(String url){
  68. InputStream is = null;
  69. String result = "";
  70. JSONObject jArray = null;
  71.  
  72. //http post
  73. try{
  74. HttpClient httpclient = new DefaultHttpClient();
  75. HttpPost httppost = new HttpPost(url);
  76. HttpResponse response = httpclient.execute(httppost);
  77. HttpEntity entity = response.getEntity();
  78. is = entity.getContent();
  79.  
  80. }catch(Exception e){
  81. Log.e("log_tag", "Error in http connection "+e.toString());
  82. }
  83.  
  84. //convert response to string
  85. try{
  86. BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  87. StringBuilder sb = new StringBuilder();
  88. String line = null;
  89. while ((line = reader.readLine()) != null) {
  90. sb.append(line + "n");
  91. }
  92. is.close();
  93. result=sb.toString();
  94. }catch(Exception e){
  95. Log.e("log_tag", "Error converting result "+e.toString());
  96. }
  97.  
  98. try{
  99.  
  100. jArray = new JSONObject(result);
  101. }catch(JSONException e){
  102. Log.e("log_tag", "Error parsing data "+e.toString());
  103. }
  104.  
  105. return jArray;
  106. }
  107. }
  108.  
  109. <uses-permission android:name="android.permission.INTERNET" />
  110. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  111. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>