Ambil data json dari android

Contoh mengambil json
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Jalan Sidomoyo",
               "short_name" : "Jl. Sidomoyo",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Sidomoyo",
               "short_name" : "Sidomoyo",
               "types" : [ "administrative_area_level_4", "political" ]
            },
            {
               "long_name" : "Godean",
               "short_name" : "Godean",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Sleman",
               "short_name" : "Kabupaten Sleman",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Daerah Istimewa Yogyakarta",
               "short_name" : "Jogja",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Indonesia",
               "short_name" : "ID",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "55264",
               "short_name" : "55264",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Jl. Sidomoyo, Godean, Sleman, Daerah Istimewa Yogyakarta 55264, Indonesia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -7.7521448,
                  "lng" : 110.3205368
               },
               "southwest" : {
                  "lat" : -7.755759599999999,
                  "lng" : 110.3204397
               }
            },
            "location" : {
               "lat" : -7.753952699999999,
               "lng" : 110.3205038
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : -7.7521448,
                  "lng" : 110.3218372302915
               },
               "southwest" : {
                  "lat" : -7.755759599999999,
                  "lng" : 110.3191392697085
               }
            }
         },
         "place_id" : "ChIJ8eqroi32ei4RT3gre1muGU0",
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "55287",
               "short_name" : "55287",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Mlati",
               "short_name" : "Mlati",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Sleman",
               "short_name" : "Kabupaten Sleman",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Daerah Istimewa Yogyakarta",
               "short_name" : "Jogja",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Indonesia",
               "short_name" : "ID",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Mlati, Sleman, Daerah Istimewa Yogyakarta 55287, Indonesia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -7.730010600000001,
                  "lng" : 110.3370819
               },
               "southwest" : {
                  "lat" : -7.7654739,
                  "lng" : 110.3046722
               }
            },
            "location" : {
               "lat" : -7.743921500000001,
               "lng" : 110.3279026
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : -7.730010600000001,
                  "lng" : 110.3370819
               },
               "southwest" : {
                  "lat" : -7.7654739,
                  "lng" : 110.3046722
               }
            }
         },
         "place_id" : "ChIJb3EaiC_2ei4RgL9TW-vFABw",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Tirtoadi",
               "short_name" : "Tirtoadi",
               "types" : [ "administrative_area_level_4", "political" ]
            },
            {
               "long_name" : "Mlati",
               "short_name" : "Mlati",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Sleman",
               "short_name" : "Kabupaten Sleman",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Daerah Istimewa Yogyakarta",
               "short_name" : "Jogja",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Indonesia",
               "short_name" : "ID",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Tirtoadi, Mlati, Sleman, Daerah Istimewa Yogyakarta, Indonesia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -7.7301001,
                  "lng" : 110.336877
               },
               "southwest" : {
                  "lat" : -7.765061,
                  "lng" : 110.3049731
               }
            },
            "location" : {
               "lat" : -7.743921500000001,
               "lng" : 110.3279026
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : -7.7301001,
                  "lng" : 110.336877
               },
               "southwest" : {
                  "lat" : -7.765061,
                  "lng" : 110.3049731
               }
            }
         },
         "place_id" : "ChIJFQm-kS_2ei4Rb3BJ_C2l1-o",
         "types" : [ "administrative_area_level_4", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Mlati",
               "short_name" : "Mlati",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Sleman",
               "short_name" : "Kabupaten Sleman",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Daerah Istimewa Yogyakarta",
               "short_name" : "Jogja",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Indonesia",
               "short_name" : "ID",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Mlati, Sleman, Daerah Istimewa Yogyakarta, Indonesia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -7.70039,
                  "lng" : 110.3870291
               },
               "southwest" : {
                  "lat" : -7.774410999999999,
                  "lng" : 110.3049731
               }
            },
            "location" : {
               "lat" : -7.7306246,
               "lng" : 110.3368391
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : -7.70039,
                  "lng" : 110.3870291
               },
               "southwest" : {
                  "lat" : -7.774410999999999,
                  "lng" : 110.3049731
               }
            }
         },
         "place_id" : "ChIJQUAkyJdYei4RUP5S43Z6AgQ",
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Sleman",
               "short_name" : "Kabupaten Sleman",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Daerah Istimewa Yogyakarta",
               "short_name" : "Jogja",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Indonesia",
               "short_name" : "ID",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Sleman, Daerah Istimewa Yogyakarta, Indonesia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -7.5418949,
                  "lng" : 110.5465291
               },
               "southwest" : {
                  "lat" : -7.8375411,
                  "lng" : 110.216021
               }
            },
            "location" : {
               "lat" : -7.732521299999999,
               "lng" : 110.402376
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : -7.5418949,
                  "lng" : 110.5465291
               },
               "southwest" : {
                  "lat" : -7.8375411,
                  "lng" : 110.216021
               }
            }
         },
         "place_id" : "ChIJSRJnxeFeei4RILxS43Z6AgM",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Daerah Istimewa Yogyakarta",
               "short_name" : "Jogja",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Indonesia",
               "short_name" : "ID",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Daerah Istimewa Yogyakarta, Indonesia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -7.541900999999999,
                  "lng" : 110.8346329
               },
               "southwest" : {
                  "lat" : -8.204188,
                  "lng" : 110.013942
               }
            },
            "location" : {
               "lat" : -7.797591499999999,
               "lng" : 110.3707141
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : -7.541900999999999,
                  "lng" : 110.8346329
               },
               "southwest" : {
                  "lat" : -8.204188,
                  "lng" : 110.013942
               }
            }
         },
         "place_id" : "ChIJxWtbvYdXei4R8LPIyrKSG20",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Indonesia",
               "short_name" : "ID",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Indonesia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 6.076912,
                  "lng" : 141.0195621
               },
               "southwest" : {
                  "lat" : -11.0074361,
                  "lng" : 95.00970699999999
               }
            },
            "location" : {
               "lat" : -0.7892749999999999,
               "lng" : 113.921327
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 5.906821,
                  "lng" : 141.018662
               },
               "southwest" : {
                  "lat" : -11.004673,
                  "lng" : 95.01106489999999
               }
            }
         },
         "place_id" : "ChIJtwRkSdcHTCwRhfStG-dNe-M",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}


// source android

                    JSONObject ret = getLocationInfo();
                    JSONObject location;
                    JSONObject location2;
                    String location_string;
                    try {
                        location = ret.getJSONArray("results").getJSONObject(0);
                        location2 = location.getJSONArray("address_components").getJSONObject(0);
                        location_string = location2.getString("long_name");
                        Log.d("test", "formattted address:" + location_string);
                        tempat_nama.setText(location_string);
                    } catch (JSONException e1) {
                        e1.printStackTrace();

                    }


methodnya

 public JSONObject getLocationInfo() {

            HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng=-7.755237,110.327535&sensor=true");
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            StringBuilder stringBuilder = new StringBuilder();

            try {
                response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();
                InputStream stream = entity.getContent();
                int b;
                while ((b = stream.read()) != -1) {
                    stringBuilder.append((char) b);
                }
            } catch (ClientProtocolException e) {
                } catch (IOException e) {
            }

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject = new JSONObject(stringBuilder.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jsonObject;
        }

Popular posts from this blog

Android C2DM (Cloud to Device Message) adalah

Menambahkan data pada TreeView delphi dari database mysql

Menampilkan tanggal fastreport delphi