java根据基站获取坐标,Android获取基站坐标代码

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我可以回答这个问题,以下是一段Java代码实现获取电话号码对应的基站信息: ``` import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class BaseStationInfo { public static void main(String[] args) throws Exception { String phoneNumber = "手机号码"; //需要查询的手机号码 String baseStationInfo = getBaseStationInfo(phoneNumber); System.out.println(baseStationInfo); } public static String getBaseStationInfo(String phoneNumber) throws Exception { String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + phoneNumber + "&co=&resource_id=6004&t=1590623724369&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110202578914251611285_1590623682635&_=1590623682636"; String result = sendGet(url); String[] splitArray = result.split("\\("); String jsonStr = splitArray[1].substring(0, splitArray[1].length() - 2); return jsonStr; } public static String sendGet(String url) throws Exception { String result = ""; BufferedReader in = null; try { URL realUrl = new URL(url); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } } ``` 注意:本代码仅供参考,具体实现需要根据自己的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值