2013年6月12日 星期三

googlemapserviceex

public void buttonPlacesClick(View v) {
try {
URL url = new URL(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyD_VBmdcP6RoEfUj-gQ2wBHpm7OxH5Qgng&location=22.997558,120.206832&radius=500&sensor=false&types=food&language=zh-TW");
InputStream is = url.openStream();
StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(is);
while (sc.hasNextLine()) {
sb.append(sc.nextLine());
}
sc.close();
is.close();
// System.out.println(sb.toString());
JSONObject root = new JSONObject(sb.toString());
JSONArray resultsArray = root.getJSONArray("results");
StringBuilder out = new StringBuilder();
for (int i = 0; i < resultsArray.length(); i++) {
JSONObject biz1 = resultsArray.getJSONObject(i);

System.out.println(biz1.getString("name"));
System.out.println(biz1.getString("vicinity"));
out.append(biz1.getString("name")+"\r\n");
// 取得經緯度
JSONObject geometryObject = biz1.getJSONObject("geometry");
JSONObject locationObject = geometryObject
.getJSONObject("location");
System.out.println("lat:" + locationObject.getDouble("lat")
+ " , lng:" + locationObject.getDouble("lng"));

}

TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(out.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

2013年6月11日 星期二

testHttpClient

public void buttonGetClick(View v) {
TextView txtOutput = (TextView) findViewById(R.id.txtOutput);

try {
// 1.建立Request URL網址
String url = "http://192.168.1.76/jiudian/test.jsp?txtName=John";

// 2.建立HttpClient物件
HttpClient getClient = new DefaultHttpClient();

// 3.建立HttpGet物件
HttpGet get = new HttpGet(url);

// 4.取得HttpResponse連線物件,並且取得連線狀態

HttpResponse response = getClient.execute(get);

// 4.1確認連線請求的結果是否為成功
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 5.建立HttpEntity物件,取得回傳的結果內容
HttpEntity entity = response.getEntity();

InputStream is = entity.getContent();

Scanner sc = new Scanner(is);

while (sc.hasNextLine()) {
txtOutput.setText(txtOutput.getText() + sc.nextLine());
}
sc.close();
is.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
txtOutput.setText(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
txtOutput.setText(e.getMessage());
}
}