package com.su.station;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONArray;import org.json.JSONObject;import android.app.Activity;import android.content.Context;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.telephony.TelephonyManager;import android.telephony.gsm.GsmCellLocation;import android.util.Log;import android.widget.Toast;public class TestStationLocationActivity extends Activity {    private static final String TAG = "TestStationLocationActivity";/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Location location = getportLocation();        location.getLongitude();        Toast.makeText(this, location.getLatitude()+location.getLatitude()+"", 100).show();            }        private Location getportLocation() {Location loc = null ;TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);BufferedReader br = null;try {GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();        if (null == gcl)        {                return null;        }            int cid = gcl.getCid();            int lac = gcl.getLac();            int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));            int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));            JSONObject holder = new JSONObject();            holder.put("version", "1.1.0");            holder.put("host", "maps.google.com");            holder.put("request_address", true);                        JSONArray array = new JSONArray();            JSONObject data = new JSONObject();                        data.put("cell_id", cid);            data.put("location_area_code", lac);            data.put("mobile_country_code", mcc);            data.put("mobile_network_code", mnc);            array.put(data);            holder.put("cell_towers", array);            DefaultHttpClient client = new DefaultHttpClient();            HttpPost post = new HttpPost("http://www.google.com/loc/json");            StringEntity se = new StringEntity(holder.toString());            post.setEntity(se);            HttpResponse resp = client.execute(post);            if (resp.getStatusLine().getStatusCode() == 200)         {                HttpEntity entity = resp.getEntity();                    br = new BufferedReader(new InputStreamReader(entity.getContent()));                    StringBuffer sb = new StringBuffer();                    String result = br.readLine();                    while (result != null)                     {                            sb.append(result);                            result = br.readLine();                    }                                        JSONObject data_ = new JSONObject(sb.toString());                    data_ = (JSONObject) data_.get("location");                loc = new Location(LocationManager.NETWORK_PROVIDER);                loc.setLatitude((Double) data_.get("latitude"));                loc.setLongitude((Double) data_.get("longitude"));                Log.i(TAG, "latitude : " + loc.getLatitude() + "  longitude : " + loc.getLongitude());                return loc;        }        return null;} catch (Exception e) {        android.util.Log.e(TAG, "network get the latitude and longitude ocurr Exception error", e);}finally{        if (null != br)        {                try                 {                        br.close();                }                 catch (IOException e)                 {                        android.util.Log.e(TAG, "network get the latitude and longitude when closed BufferedReader ocurr IOException error", e);                }        }}return loc;}}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android渐变色xml文件
  2. ch024 Android BroadCastReceiver
  3. android 获取手机的所有程序和widget的包
  4. Android 透明度对应16进制值
  5. Android Theme 样式 展示
  6. android 随手记-画虚线
  7. Android 允许权限
  8. android中文字跑马灯效果
  9. Android日期时间格式国际化
  10. android 颜色值 xml