android 基站定位
2014-03-17 23:15
274 查看
package cn.LocationStation;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
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.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LocationStation extends Activity {
TextView mTextView;
Button mButton;
TelephonyManager tm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.textView001);
mButton = (Button) findViewById(R.id.Button001);
tm = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
mButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
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));
try {
// 组装JSON查询字符串
JSONObject holder = new JSONObject();
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
// holder.put("address_language", "zh_CN");
holder.put("request_address", true);
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
data.put("cell_id", cid); // 25070
data.put("location_area_code", lac);// 4474
data.put("mobile_country_code", mcc);// 460
data.put("mobile_network_code", mnc);// 0
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);
HttpEntity entity = resp.getEntity();
BufferedReader 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 jsonObject = new JSONObject(sb.toString());
JSONObject jsonObject1 = new JSONObject(jsonObject
.getString("location"));
getAddress(jsonObject1.getString("latitude"), jsonObject1
.getString("longitude"));
//mTextView.setText(sb.toString());
} catch (Exception e) {
// TODO: handle exception
}
}
});
}
void getAddress(String lat, String lag) {
try {
URL url = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="
+ lat + "," + lag);
InputStream inputStream = url.openConnection().getInputStream();
InputStreamReader inputReader = new InputStreamReader(inputStream,
"utf-8");
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "", lines = "";
while ((line = bufReader.readLine()) != null) {
lines += line;
}
if (!lines.equals("")) {
JSONObject jsonobject = new JSONObject(lines);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")
.toString());
for (int i = 0; i < jsonArray.length(); i++) {
mTextView.setText(mTextView.getText() + "\n"
+ jsonArray.getJSONObject(i).getString("address"));
}
}
} catch (Exception e) {
;
}
}
}
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
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.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LocationStation extends Activity {
TextView mTextView;
Button mButton;
TelephonyManager tm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.textView001);
mButton = (Button) findViewById(R.id.Button001);
tm = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
mButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
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));
try {
// 组装JSON查询字符串
JSONObject holder = new JSONObject();
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
// holder.put("address_language", "zh_CN");
holder.put("request_address", true);
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
data.put("cell_id", cid); // 25070
data.put("location_area_code", lac);// 4474
data.put("mobile_country_code", mcc);// 460
data.put("mobile_network_code", mnc);// 0
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);
HttpEntity entity = resp.getEntity();
BufferedReader 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 jsonObject = new JSONObject(sb.toString());
JSONObject jsonObject1 = new JSONObject(jsonObject
.getString("location"));
getAddress(jsonObject1.getString("latitude"), jsonObject1
.getString("longitude"));
//mTextView.setText(sb.toString());
} catch (Exception e) {
// TODO: handle exception
}
}
});
}
void getAddress(String lat, String lag) {
try {
URL url = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="
+ lat + "," + lag);
InputStream inputStream = url.openConnection().getInputStream();
InputStreamReader inputReader = new InputStreamReader(inputStream,
"utf-8");
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "", lines = "";
while ((line = bufReader.readLine()) != null) {
lines += line;
}
if (!lines.equals("")) {
JSONObject jsonobject = new JSONObject(lines);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")
.toString());
for (int i = 0; i < jsonArray.length(); i++) {
mTextView.setText(mTextView.getText() + "\n"
+ jsonArray.getJSONObject(i).getString("address"));
}
}
} catch (Exception e) {
;
}
}
}
相关文章推荐
- 【Android开发笔记】4.简单基站定位程序
- Android GSM+CDMA基站定位
- Android 简单基站定位程序
- 在Android里完美实现基站和WIFI定位
- android开发通过基站手机网络实现定位功能(Google实现)
- Android基站定位——通过手机信号获取基站信息(一)
- 在Android里完美实现基站和WIFI定位
- Android基站定位——单基站定位(二)
- Android 边用边学 之 基站定位
- Android获取手机基站信息并进行基站定位(基站定位原理)
- WIFI之一:WIFI常识 && ANDROID平台上调试APM6633 && 基站定位原理
- android定位使用基站
- android gps ,wifi ,基站定位集合
- android获取基站定位信息的文章收集
- Android基站定位
- android 定位 WIFI定位其实和基站定位
- 在Android里完美实现基站和WIFI定位
- Android基站定位——通过手机信号获取基站信息(一)
- Android基站定位
- Android基站+联网+google数据库定位