More Related Content
Similar to AndroidからWebサービスを使う
Similar to AndroidからWebサービスを使う (20)
More from Masafumi Terazono
More from Masafumi Terazono (20)
AndroidからWebサービスを使う
- 21. • Ecrair/Froyo Apache HTTP Client
• Gingerbread HttpURLConnection API
Android
• HttpURLConnection
Re:Kayo-System Co.,Ltd.
2012 2 15
- 35. package jp.co.kayo.android.localplayer.ds.ampache.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import android.test.AndroidTestCase;
import android.util.Log;
public class MapAPITest extends AndroidTestCase {
final String TAG = "MapAPI Test";
public void testCallService() {
try {
URI uri = new URI("http://maps.google.com/maps/api/geocode/json?address=".concat(
URLEncoder.encode(" ")).concat("&sensor=false"));
HttpURLConnection con = (HttpURLConnection) uri.toURL().openConnection();
InputStream is = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
Log.d("", line);
}
is.close();
reader.close();
} catch (URISyntaxException e) {
Log.d(TAG, "URI Syntax error", e);
} catch (MalformedURLException e) {
Log.d(TAG, "URL Malformed Error", e);
} catch (IOException e) {
Log.d(TAG, "IO error", e);
}
}
}
Re:Kayo-System Co.,Ltd.
2012 2 15
- 37. try {
URI uri = new URI("http://maps.google.com/maps/api/geocode/json?address=".concat(
URLEncoder.encode(" ")).concat("&sensor=false"));
HttpURLConnection con = (HttpURLConnection) uri.toURL().openConnection();
InputStream is = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder json = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
Log.d("", line);
json.append(line);
}
is.close();
reader.close();
} catch (URISyntaxException e) {
Log.d(TAG, "URI Syntax error", e);
} catch (MalformedURLException e) {
Log.d(TAG, "URL Malformed Error", e);
} catch (IOException e) {
Log.d(TAG, "IO error", e);
}
Re:Kayo-System Co.,Ltd.
2012 2 15
- 38. StringBuilder json = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
Log.d("", line);
json.append(line);
}
{
"results" : [
{
"address_components" : [
{
"long_name" : " ",
"short_name" : " ",
"types" : [ "locality", "political" ]
},
{
"long_name" : " ",
"short_name" : " ",
"types" : [ "administrative_area_level_1", "political" ]
},
Re:Kayo-System Co.,Ltd.
2012 2 15
- 39. {
"long_name" : " ",
"short_name" : " ",
"types" : [ "locality", "political" ]
}
"formatted_address" : " , ",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 31.63195899999999,
"lng" : 130.82772330
},
"southwest" : {
"lat" : 31.41453820,
"lng" : 130.68629260
}
},
"location" : {
"lat" : 31.49273440,
"lng" : 130.70091430
},
Re:Kayo-System Co.,Ltd.
2012 2 15
- 40. private void printAddress(String str){
try {
JSONObject json = new JSONObject(str);
JSONArray results = json.getJSONArray("results");
int count = results.length();
for(int i=0; i<count; i++){
JSONObject item = results.getJSONObject(i);
String formatted_address = item.getString("formatted_address");
JSONObject geom = item.getJSONObject("geometry");
JSONObject location = geom.getJSONObject("location");
String lat = location.getString("lat");
String lng = location.getString("lng");
Log.d(TAG, "formatted_address"+ formatted_address);
Log.d(TAG, "(lat, lng)="+lat+","+lng);
}
} catch (JSONException e) {
Log.d(TAG, "Json Error", e);
}
}
Re:Kayo-System Co.,Ltd.
2012 2 15