MainActivity
public class MainActivity extends Activity {
private static final String SCHEME = "https";
private static final String AUTHORITY = "maps.googleapis.com";
private static final String PATH = "maps/api/geocode/json";
private static final String TAG = "MainActivity";
ArrayAdapter<Location> adapter;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
textView = (TextView)findViewById(R.id.text_result);
}
public void onClickButton(View v){
Builder builder = new Builder();
builder.scheme(SCHEME);
builder.authority(AUTHORITY);
builder.path(PATH);
builder.appendQueryParameter("address", "東京");
builder.appendQueryParameter("sensor", "true");
builder.appendQueryParameter("region", "jp");
builder.appendQueryParameter("language", "ja");
String uri = Uri.decode(builder.build().toString());
Log.v(TAG, uri);
try {
String responce = HttpHelper.getResponseContent(uri);
textView.setText(responce);
ArrayList<Location> list = JsonHelper.toLocations(responce);
adapter = new ArrayAdapter<Location>(this, android.R.layout.simple_list_item_1, list);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Title");
dialogBuilder.setSingleChoiceItems(adapter, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, adapter.getItem(which).name + ":" + adapter.getItem(which).lat, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialogBuilder.setNegativeButton(android.R.string.cancel, null);
dialogBuilder.show();
} catch (HttpHelperException e) {
Log.e(TAG, e.getMessage(), e);
} catch (JSONException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
HttpHelper
public class HttpHelper {
public static final int TIME_OUT = 30000;
private static final String TAG = "HttpHelper";
public static String getResponseContent(String url) throws HttpHelperException {
Log.v(TAG, ">>>>>>>> Start HttpRequest <<<<<<<<");
Log.v(TAG, "request for " + url);
String responce = null;
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = client.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = httpResponse.getEntity();
responce = EntityUtils.toString(entity);
Log.v(TAG, "__Responce:" + responce);
} else {
Log.e(TAG, "## Request error. url:" + url + " code:" + statusCode);
throw new HttpHelperException("Connection Failed");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
throw new HttpHelperException("Connection Failed:" + e.getMessage());
}
Log.v(TAG, ">>>>>>>> End HttpRequest <<<<<<<<");
return responce;
}
}
JsonHelper
public class JsonHelper {
private static final String TAG = "JsonHelper";
private static final String PREFIX = "日本, ";
public static JSONObject toJson(String str) throws JSONException {
if (TextUtils.isEmpty(str)) {
throw new JSONException("string is null");
}
JSONObject json = null;
try {
json = new JSONObject(str);
} catch (JSONException e) {
Log.e(TAG, "-toJson- ", e);
}
return json;
}
public static ArrayList<Location> toLocations(String s) throws JSONException {
ArrayList<Location> locations = new ArrayList<Location>();
JSONObject json = toJson(s);
JSONArray results = json.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
locations.add(toLocation(result));
}
for(Location location:locations){
Log.v("", location.toStr());
}
return locations;
}
public static Location toLocation(JSONObject result) throws JSONException {
Location location = new Location();
String formatted_address = result.getString("formatted_address");
if (formatted_address.startsWith(PREFIX)) {
location.name = formatted_address.replace(PREFIX, "");
} else {
location.name = formatted_address;
}
JSONObject geometry = result.getJSONObject("geometry");
JSONObject jsonLocation = geometry.getJSONObject("location");
location.lat = jsonLocation.getDouble("lat");
location.lng = jsonLocation.getDouble("lng");
return location;
}
}
Location
public class Location {
public String name;
public double lat;
public double lng;
public String toStr() {
return "Location [name=" + name + ", lat=" + lat + ", lng=" + lng + "]";
}
@Override
public String toString() {
return name;
}
}