我试图通过 gps 获取地址,将坐标转换为我的位置。我正在使用这段代码:
latitude1 = (location.getLatitude());
longitude2= (location.getLongitude());
JSONObject ret = getLocationInfo();
JSONObject location2;
String location_string;
try {
location2 = ret.getJSONArray("results").getJSONObject(0);
location_string = location2.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
StringRua = (" " + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
和
public JSONObject getLocationInfo()
{
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+latitude1+","+longitude2+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
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) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
它在我的手机中运行良好。但是当我的朋友在执行这部分代码时尝试使用同一个应用程序时,应用程序只是强制关闭。
请记住,我已经尝试使用:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude1, longitude2, 1);
但是执行这个命令太慢了,事实上,它总是返回 null。
谁能帮我?谢谢你
编辑:我刚刚更改了我的代码以使用 asynctask,但现在我没有正确获取位置,它只是说空。
这是我所做的:
private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.show();
}
@Override
protected Void doInBackground(Void... params) {
//this method will be running on background thread so don't update UI frome here
//do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
JSONObject ret = getLocationInfo();
JSONObject location2;
String location_string;
try {
location2 = ret.getJSONArray("results").getJSONObject(0);
location_string = location2.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
StringRua = (" " + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
return null;
}
异步任务调用,它位于 AsyncTask 之上:
public JSONObject getLocationInfo() {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+latitude1+","+longitude2+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
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) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
我正在使用,调用 Async :
new AsyncCaller().execute();
" StringRua = (" " + location_string); " 没有得到任何位置
编辑 2:我已将公共 JSONObject getLocationInfo() 放入私有类 AsyncCaller extends AsyncTask {
我只是发现它第一次调用 asynctask 时没有获得位置,但第二次它工作。但我需要在它第一次调用 asynctask 时完成这项工作。OBS:在我朋友的手机中,它不再崩溃,但他总是得到 Null