java根据电话号码获取基站信息代码实现
时间: 2024-02-01 22:02:22 浏览: 124
获取基站信息
我可以回答这个问题,以下是一段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;
}
}
```
注意:本代码仅供参考,具体实现需要根据自己的需求进行调整。
阅读全文