我有这个代码:
jsonObject1=getLocationInfo(direction);
它旨在返回一个 jsonObject,稍后将对其进行解析,我将获得纬度和经度。
这是方法:
public JSONObject getLocationInfo(final String address) {
Thread thread=new Thread(){
public void run(){
String adressString=address.replaceAll("\\s+",",");
StringBuilder stringBuilder = new StringBuilder();
try {
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + adressString + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
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) {
}
;
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread.start();
return jsonObject;
}
问题是......由于该方法涉及一个 HttpGet,它必须在另一个线程中。但是,就在 thread.start() 方法之后,调用了 return jsonObject 行,所以返回的对象将为空......我怎么能在返回任何东西之前等待线程结束?
谢谢你。