packagecom.utils;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.List;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importorg.json.JSONStringer;importandroid.content.Context;importandroid.telephony.NeighboringCellInfo;importandroid.telephony.TelephonyManager;publicclassLocation {publicstaticString LOCATIONS_URL="http://www.google.com/loc/json";publicstaticString getLocations(Context context) {//generate json requestString jr=generateJsonRequest(context);try{
DefaultHttpClient client=newDefaultHttpClient();
StringEntity entity=newStringEntity(jr);
HttpPost httpost=newHttpPost(LOCATIONS_URL);
httpost.setEntity(entity);
HttpResponse response=client.execute(httpost);
String locationsJSONString=getStringFromHttp(response.getEntity());returnextractLocationsFromJsonString(locationsJSONString);
}catch(ClientProtocolException e) {//e.printStackTrace();}catch(IOException e) {//e.printStackTrace();}catch(Exception e) {//e.printStackTrace();}returnnull;
}privatestaticString extractLocationsFromJsonString(String jsonString) {
String country="";
String region="";
String city="";
String street="";
String street_number="";doublelatitude=0.0;doublelongitude=0.0;//"accuracy":901.0doubleaccuracy=0.0;try{
JSONObject jo=newJSONObject(jsonString);
JSONObject location=(JSONObject) jo.get("location");
latitude=(Double) location.get("latitude");
longitude=(Double) location.get("longitude");
accuracy=(Double) location.get("accuracy");
JSONObject address=(JSONObject) location.get("address");
country=(String) address.get("country");
region=(String) address.get("region");
city=(String) address.get("city");
street=(String) address.get("street");
street_number=(String) address.get("street_number");
}catch(JSONException e) {//e.printStackTrace();}return"("+latitude+","+longitude+")\t"+country+region+city+street+street_number+"\t"+"精确度:"+accuracy;
}//获取所有的网页信息以String 返回privatestaticString getStringFromHttp(HttpEntity entity) {
StringBuffer buffer=newStringBuffer();try{//获取输入流BufferedReader reader=newBufferedReader(newInputStreamReader(
entity.getContent()));//将返回的数据读到buffer中String temp=null;while((temp=reader.readLine())!=null) {
buffer.append(temp);
}
}catch(IllegalStateException e) {//e.printStackTrace();}catch(IOException e) {//e.printStackTrace();}returnbuffer.toString();
}privatestaticString generateJsonRequest(Context context) {
TelephonyManager manager=(TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
ListcellList=manager.getNeighboringCellInfo();if(cellList.size()==0)returnnull;
JSONStringer js=newJSONStringer();try{
js.object();
js.key("version").value("1.1.0");
js.key("host").value("maps.google.com");
js.key("home_mobile_country_code").value(460);
js.key("home_mobile_network_code").value(0);
js.key("radio_type").value("gsm");
js.key("request_address").value(true);
js.key("address_language").value("zh_CN");
JSONArray ct=newJSONArray();for(NeighboringCellInfo info : cellList) {
JSONObject c=newJSONObject();
c.put("cell_id", info.getCid());
c.put("location_area_code", info.getLac());
c.put("mobile_country_code",460);
c.put("mobile_network_code",0);
c.put("signal_strength", info.getRssi());//获取邻居小区信号强度ct.put(c);
}
js.key("cell_towers").value(ct);
js.endObject();
}catch(JSONException e) {//e.printStackTrace();returnnull;
}returnjs.toString().replace("true","True");
}
}