基站定位代码封装

在真机上测试:
要点一: 一定要插手机卡  ---得到cid通过移动
要点二: 一定要能上网    ----得到经纬度---通过google

public class BaseStationManager { Context c; private TelephonyManager phoneService; String json = "aaa"; private static volatile BaseStationManager bsManager ; private BaseStationManager(Context c){ this.c = c; } public static BaseStationManager getInstance(Context c){ if(bsManager == null){ synchronized(BaseStationManager.class){ if(bsManager == null){ bsManager = new BaseStationManager(c); } } }//if return bsManager; } //======================================================================================= public void init(){ phoneService = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE); update(); } public String getLatitude(){ String latitude = "null"; Pattern p = Pattern.compile("latitude/":[^,]+"); Matcher m = p.matcher(json); if(m.find()){ latitude = m.group().split(":")[1]; } return latitude; } public String getLongitude(){ String latitude = "null"; Pattern p = Pattern.compile("longitude/":[^,]+"); Matcher m = p.matcher(json); if(m.find()){ latitude = m.group().split(":")[1]; } return latitude; } public void update(){ GsmCellLocation gcl = (GsmCellLocation) phoneService.getCellLocation(); int cid = gcl.getCid();//CID for GSM int lac = gcl.getLac(); int mcc = Integer.valueOf(phoneService.getNetworkOperator().substring(0,3)); int mnc = Integer.valueOf(phoneService.getNetworkOperator().substring(3,5)); json = "bbb"; try { //组装JSON查询字符串 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); //=============json封装成StringEntity,StringEntity让入post, post让client执行 // 创建连接,发送请求并接受回应 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); HttpEntity entity = resp.getEntity(); BufferedReader br = new BufferedReader( new InputStreamReader(entity.getContent())); StringBuffer sb = new StringBuffer(); String l = null; while ((l=br.readLine()) != null) { sb.append(l); } json = sb.toString(); }catch(Exception e){ } } }

你可能感兴趣的:(json,exception,String,null,mobile,NetWork)