学习了一下反向地理编码,但是服务器无响应总是504错误
下面主要代码:
package com.adolph.locationtesttwo;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView positionTextView;
private LocationManager loacationManager;
private String provider;
public static final int SHOW_LOCATION = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positionTextView = (TextView)findViewById(R.id.position_text_view);
loacationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);// 获取的是位置服务
//获取所有可用的位置提供器
List<String> providerList = loacationManager.getProviders(true);//得到所有可用的位置提供器
if (providerList.contains(LocationManager.GPS_PROVIDER)){
provider = LocationManager.GPS_PROVIDER;
}else if (providerList.contains(LocationManager.NETWORK_PROVIDER)){
provider= LocationManager.NETWORK_PROVIDER;
}else{
Toast.makeText(this,"No location provider to use",Toast.LENGTH_SHORT).show();
return;
}
Location location = loacationManager.getLastKnownLocation(provider);//获取到记录当前位置信息的Location对象了
if (location != null){
//显示当前设备的位置信息
showLocation(location);
}
loacationManager.requestLocationUpdates(provider,5000,1,locationListener);//实现位置实时更新
}
protected void onDestroy(){
super.onDestroy();
if (loacationManager != null){
//关闭程序时将监听器移除
loacationManager.removeUpdates(locationListener);
}
}
LocationListener locationListener = new LocationListener(){
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
// Provider的在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onLocationChanged(Location location) {
// 更新当前设备的位置信息
showLocation(location);
}
};
private void showLocation(final Location location) {
new Thread(new Runnable() {
@Override
public void run() {
try {
// 组装反向地理编码的接口地址
StringBuilder url = new StringBuilder();
url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
url.append(location.getLatitude()).append(",");
url.append(location.getLongitude());
url.append("&sensor=false");
Log.i("xxx",url.toString());
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
// 在请求消息头中指定语言,保证服务器会返回中文数据
httpGet.addHeader("Accept-Language", "zh-CN");
HttpResponse httpResponse = httpClient.execute(httpGet);
Log.d("xxx","11");
String q = httpResponse.getStatusLine().getStatusCode()+"w";
Log.i("xxx",q);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
Log.d("xxx","12");
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
JSONObject jsonObject = new JSONObject(response);
// 获取results节点下的位置信息
JSONArray resultArray = jsonObject.getJSONArray ("results");
if (resultArray.length() > 0) {
JSONObject subObject = resultArray. getJSONObject(0);
// 取出格式化后的位置信息
String address = subObject.getString ("formatted_address");
Log.i("xxx",address);
Message message = new Message();
message.what = SHOW_LOCATION;
message.obj = address;
handler.sendMessage(message);
}
}
Log.d("xxx","13");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_LOCATION:
String currentPosition = (String) msg.obj;
positionTextView.setText(currentPosition);
break;
default:
break;
}
}
};
// private void showLocation(Location location) {
// String currentPosition = "latitude is " + location.getLatitude() + "\n"
// + "longitude is " + location.getLongitude();
// positionTextView.setText(currentPosition);
// }
}