003 | import java.io.BufferedReader; |
004 | import java.io.IOException; |
005 | import java.io.InputStreamReader; |
007 | import org.apache.http.HttpEntity; |
008 | import org.apache.http.HttpResponse; |
009 | import org.apache.http.client.methods.HttpPost; |
010 | import org.apache.http.entity.StringEntity; |
011 | import org.apache.http.impl.client.DefaultHttpClient; |
012 | import org.json.JSONArray; |
013 | import org.json.JSONObject; |
015 | import android.app.Activity; |
016 | import android.content.Context; |
017 | import android.location.Location; |
018 | import android.location.LocationManager; |
019 | import android.os.Bundle; |
020 | import android.telephony.TelephonyManager; |
021 | import android.telephony.gsm.GsmCellLocation; |
022 | import android.util.Log; |
023 | import android.widget.Toast; |
025 | public class TestStationLocationActivity extends
Activity { |
026 | private
static final String TAG = "TestStationLocationActivity" ; |
028 | /** Called when the activity is first created. */ |
030 | public
void onCreate(Bundle savedInstanceState) { |
031 | super .onCreate(savedInstanceState); |
032 | setContentView(R.layout.main); |
033 | Location location = getportLocation(); |
034 | location.getLongitude(); |
035 | Toast.makeText( this , location.getLatitude()+location.getLatitude()+ "" , 100 ).show();
|
039 | private
Location getportLocation() { |
041 | TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
042 | BufferedReader br = null ; |
045 | GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation(); |
050 | int
cid = gcl.getCid(); |
051 | int
lac = gcl.getLac(); |
052 | int
mcc = Integer.valueOf(tm.getNetworkOperator().substring( 0 , 3 )); |
053 | int
mnc = Integer.valueOf(tm.getNetworkOperator().substring( 3 , 5 )); |
054 | JSONObject holder = new JSONObject(); |
055 | holder.put( "version" , "1.1.0" );
|
056 | holder.put( "host" , "maps.google.com" ); |
057 | holder.put( "request_address" , true ); |
059 | JSONArray array = new JSONArray(); |
060 | JSONObject data = new JSONObject(); |
062 | data.put( "cell_id" , cid); |
063 | data.put( "location_area_code" , lac); |
064 | data.put( "mobile_country_code" , mcc); |
065 | data.put( "mobile_network_code" , mnc); |
067 | holder.put( "cell_towers" , array); |
068 | DefaultHttpClient client = new DefaultHttpClient(); |
069 | HttpPost post = new HttpPost( "http://www.google.com/loc/json" ); |
070 | StringEntity se = new StringEntity(holder.toString()); |
072 | HttpResponse resp = client.execute(post); |
073 | if
(resp.getStatusLine().getStatusCode() == 200 ) |
075 | HttpEntity entity = resp.getEntity(); |
076 | br =
new BufferedReader( new
InputStreamReader(entity.getContent())); |
077 | StringBuffer sb = new StringBuffer(); |
078 | String result = br.readLine(); |
082 | result = br.readLine(); |
085 | JSONObject data_ = new JSONObject(sb.toString()); |
086 | data_ = (JSONObject) data_.get( "location" ); |
087 | loc =
new Location(LocationManager.NETWORK_PROVIDER); |
088 | loc.setLatitude((Double) data_.get( "latitude" )); |
089 | loc.setLongitude((Double) data_.get( "longitude" )); |
090 | Log.i(TAG, "latitude : " +loc.getLatitude() + " longitude : " +loc.getLongitude());
|
099 | android.util.Log.e(TAG, "network get the latitude and longitude ocurr Exception error" , e); |
111 | android.util.Log.e(TAG, "network get the latitude and longitude when closed BufferedReader ocurr IOException error" , e); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理