我想在不使用 GPS 的情况下获取我的黑莓设备的位置。所以,我使用 Opencellid 来获得结果。但它给了我错误的位置结果。谁能澄清确切的程序是什么以及我可能做错了什么?
我的代码:
public final static String getQueryString(){
cellID = Integer.toString(GPRSInfo.getCellInfo().getCellId());
// Retrieves the Location Area Code.
lac = Integer.toString(GPRSInfo.getCellInfo().getLAC());
// Retrieves the mobile country code.
mcc = Integer.toHexString(RadioInfo.getMCC(RadioInfo.getCurrentNetworkIndex()));
// Retrieves the Location network Code.
mnc = Integer.toHexString(RadioInfo.getMNC(RadioInfo.getCurrentNetworkIndex()));
queryStr="http://www.opencellid.org/cell/get?
key="+myapikey+"&mcc="+mcc+"&mnc="+mnc+"&cellid="+cellID+"&lac="+lac+"&fmt=txt";
return queryStr;
}
public void httpGetRequest(){
HttpConnection conn = null;
InputStream in = null; StringBuffer buff = new StringBuffer(); String result = "";
try {
conn=(HttpConnection) Connector.open(getQueryString()+getString(),Connector.READ);
conn.setRequestMethod(HttpConnection.GET); conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
conn.setRequestProperty("Content-Type", "plain/text");
in = conn.openInputStream();
int car;
while( (car=in.read())!= -1){
buff.append((char)car);
}
in.close();
conn.close();
result=buff.toString();
//get latitude and longitude
if(result.startsWith("err")){
System.out.println("Cell not found!");
}else{
int pos=result.indexOf(',');
String lat=result.substring(0,pos);
int pos2=result.indexOf(',',pos+1);
String lon=result.substring(pos+1,pos2);
System.out.println(lat+" "+lon);
getLocationFromGoogleMaps(lat,lon);
}
} catch (Exception ex) {
System.out.println("====Exception: "+ex.getMessage());
} finally {
try {
if (in != null) {
in.close();
}
conn.close();
} catch (IOException e) {
System.out.println("====Exception: "+e.getMessage());
}
}
}