我有一个设备向我发送它的位置。它通常发送从GPS获得的坐标,但如果GPS信号不好,则进行GSM三角测量并向我发送MCC,MNC,LAC和CELLID值。
拥有这4个值,我需要获得位置坐标(纬度,经度)。为实现这一目标,我正在尝试使用谷歌的GLM隐藏服务(http://www.google.com/glm/mmap)。假设将这4个值赋予该服务,它返回纬度/经度坐标,但网上没有太多关于此服务的信息。
通过重新选择的信息,我编写了一个小应用程序来执行此操作,但无法正常工作。这就是Locgact所显示的:
11-21 13:02:29.692: W/IInputConnectionWrapper(11990): endBatchEdit on inactive InputConnection
11-21 13:02:31.264: W/IInputConnectionWrapper(11990): endBatchEdit on inactive InputConnection
11-21 13:02:43.506: W/System.err(11990): android.os.NetworkOnMainThreadException
11-21 13:02:43.506: W/System.err(11990): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
11-21 13:02:43.516: W/System.err(11990): at com.gsmlocation.MainActivity.transform(MainActivity.java:114)
11-21 13:02:43.516: W/System.err(11990): at com.gsmlocation.MainActivity$1.onClick(MainActivity.java:80)
11-21 13:02:43.516: W/System.err(11990): at android.view.View.performClick(View.java:4445)
11-21 13:02:43.526: W/System.err(11990): at android.view.View$PerformClick.run(View.java:18446)
11-21 13:02:43.526: W/System.err(11990): at android.os.Handler.handleCallback(Handler.java:733)
11-21 13:02:43.526: W/System.err(11990): at android.os.Handler.dispatchMessage(Handler.java:95)
11-21 13:02:43.526: W/System.err(11990): at android.os.Looper.loop(Looper.java:136)
11-21 13:02:43.526: W/System.err(11990): at android.app.ActivityThread.main(ActivityThread.java:5146)
11-21 13:02:43.536: W/System.err(11990): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 13:02:43.536: W/System.err(11990): at java.lang.reflect.Method.invoke(Method.java:515)
11-21 13:02:43.536: W/System.err(11990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-21 13:02:43.536: W/System.err(11990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-21 13:02:43.536: W/System.err(11990): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
public Location transform(int cellID, int lac, int mcc, int mnc) {
try {
URL providerAddress = new URL("http://www.google.com/glm/mmap");
HttpURLConnection connection = (HttpURLConnection) providerAddress.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
OutputStream outputStream = connection.getOutputStream();
writePlainData(outputStream, cellID, lac, mcc, mnc);
InputStream inputStream = connection.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
double lat = dataInputStream.readInt() / 1000000D;
double lon = dataInputStream.readInt() / 1000000D;
coordinates.setLatitude(lat);
coordinates.setLongitude(lon);
return coordinates;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void writePlainData(OutputStream out, int cellID, int lac, int mcc, int mnc) throws IOException {
DataOutputStream dos = new DataOutputStream(out);
dos.writeShort(0x0E); // Fct code
dos.writeInt(0); // requesting 8 byte session
dos.writeInt(0);
dos.writeShort(0); // country code string
dos.writeShort(0); // client descriptor string
dos.writeShort(0); // version tag string
dos.writeByte(0x1B); // Fct code
dos.writeInt(0); // MNC?
dos.writeInt(0); // MCC?
dos.writeInt(3); // Radio Access Type (3=GSM, 5=UMTS)
dos.writeShort(0); // length of provider name
// provider name string
dos.writeInt(cellID); // CID
dos.writeInt(lac); // LAC
dos.writeInt(mnc); // MNC
dos.writeInt(mcc); // MCC
dos.writeInt(-1); // always -1
dos.writeInt(0); // rx level
dos.flush();
}
LocCat指向的行是:
connection.connect();
答案 0 :(得分:1)
u r geating android.os.NetworkOnMainThreadException
因此请在AsyncTask
class LoadingLocation extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
public Location transform(int cellID, int lac, int mcc, int mnc) {
try {
URL providerAddress = new URL("http://www.google.com/glm/mmap");
HttpURLConnection connection = (HttpURLConnection) providerAddress.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
OutputStream outputStream = connection.getOutputStream();
writePlainData(outputStream, cellID, lac, mcc, mnc);
InputStream inputStream = connection.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
double lat = dataInputStream.readInt() / 1000000D;
double lon = dataInputStream.readInt() / 1000000D;
coordinates.setLatitude(lat);
coordinates.setLongitude(lon);
return coordinates;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
return null;
}
}