저는 Android에서 매우 새로운데 도시에서 좌표를 검색하려고합니다. 아래의 코드를 다양한 자습서에 따라 작성했습니다. 그것의 문제는 행을 실행 한 후 "HttpResponse response = client.execute (request);" 그것은 exceptio에 직접적으로 뛰어 오르고 정말 이유를 이해할 수 없습니다. 나는 지난 이틀 동안 이걸로 힘들게 지내왔다. 내가 매니페스트 파일 및 프로젝트 빌드 대상에 추가 한좌표 검색을 시도하는 중 HttpResponse를 사용하는 중에 예외가 발생했습니다.
안드로이드 사물의 이러한 종류의 작업을 수행하는 지오 코더의 빌드를 가지고 구글 API 4.2.2
public double[] searchCoordinate(String city) {
double[] coordinates = new double[2];
String petHTTP1 = "http://maps.googleapis.com/maps/api/geocode/json?address=";
String petHTTP2 = "&sensor=false";
String petHTTP = petHTTP1 + city + petHTTP2;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(petHTTP));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null){
sb.append(line);
}
in.close();
System.out.println(sb.toString());
JSONObject jsonObject = new JSONObject(sb.toString());
String resp = jsonObject.getString("status");
if (resp.equals("OK")) {
JSONArray array = jsonObject.getJSONArray("results");
JSONObject item = array.getJSONObject(0);
JSONObject point = item.getJSONObject("geometry").getJSONObject("location");
coordinates[0] = point.getDouble("lat");
coordinates[1] = point.getDouble("lng");
System.out.println("Longitude: "+coordinates[0]+" - Latitude: "+coordinates[1]);
}
}catch (Exception e) {
e.printStackTrace();
}
return coordinates;
}
당신은 스레드 나 AsyncTask를의 코드를 실행하고 있습니까? 그렇지 않으면 주 스레드에서 인터넷 작업을 수행하려고 시도하는 것에 대해 불평 할 것입니다 (UI 고정을 만들 수 있음). – cYrixmorten
아니요, 저는 아닙니다 ... 스레드 또는 AsyncTask에 대해 처음 들었습니다. 그 중 하나에서 코드를 실행하는 것이 좋습니다. 많은 감사 – EvaBT9