在Android中无法将Cell ID和Lac转换为纬度和经度

时间:2016-01-02 06:36:40

标签: java android google-maps

我正在尝试在我的应用中获取所有用户单元格和位置信息,并将它们显示在一些TextView中。我正确地获取了所有信息,但它并没有返回纬度和经度。它显示“找不到位置”。我查了一下,发现我的“httpConn.Connect()”语句抛出异常。我不是为什么它会抛出异常。我已经提供了所需的所有权限。我该怎么办?

这是我的代码:

public class CellActivity extends AppCompatActivity {

private static String TAG = CellActivity.class.getSimpleName();
int myLatitude,myLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cell);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TelephonyManager telephonyManager=(TelephonyManager) this.getSystemService(this.TELEPHONY_SERVICE);
    String serialNum=telephonyManager.getSimSerialNumber();
    String deviceId=telephonyManager.getDeviceId();
    GsmCellLocation location=(GsmCellLocation) telephonyManager.getCellLocation();
    int cid=location.getCid();
    int lac=location.getLac();
    String networkOperator = telephonyManager.getNetworkOperator();
    int mcc = Integer.parseInt(networkOperator.substring(0, 3));
    int mnc = Integer.parseInt(networkOperator.substring(3));
    String oName=telephonyManager.getNetworkOperatorName();

    TextView sim=(TextView) findViewById(R.id.sim);
    TextView deviceID=(TextView) findViewById(R.id.deviceId);
    TextView cellId=(TextView) findViewById(R.id.cellId);
    TextView mccb=(TextView) findViewById(R.id.mcc);
    TextView mncb=(TextView) findViewById(R.id.mnc);
    TextView operator=(TextView) findViewById(R.id.operator);
    TextView lacb=(TextView) findViewById(R.id.lac);
    TextView operatorName=(TextView) findViewById(R.id.operatorName);
    TextView position=(TextView) findViewById(R.id.position);

    sim.setText("serial no. : "+serialNum);
    deviceID.setText("device id : "+deviceId);
    cellId.setText("cid : "+cid);
    mccb.setText("mcc : "+mcc);
    mncb.setText("mnc : "+mnc);
    operator.setText("operator : "+networkOperator);
    lacb.setText("lac : "+lac);
    operatorName.setText(oName);

    if(RqsLocation(cid, lac)){
        position.setText(
                String.valueOf((float)myLatitude/1000000)
                        + " : "
                        + String.valueOf((float)myLongitude/1000000));
    }else{
        position.setText("Can't find Location!");
    }
}

private Boolean RqsLocation(final int cid,final int lac){

    Boolean result = false;

    final String urlmmap = "http://www.google.com/glm/mmap";

    try {
        URL url = new URL(urlmmap);
        URLConnection conn = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        try {
            httpConn.connect();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        OutputStream outputStream = httpConn.getOutputStream();
        WriteData(outputStream, cid, lac);

        InputStream inputStream = httpConn.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        dataInputStream.readShort();
        dataInputStream.readByte();
        int code = dataInputStream.readInt();
        if (code == 0) {
            myLatitude = dataInputStream.readInt();
            myLongitude = dataInputStream.readInt();

            result = true;

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;

}

private void WriteData(OutputStream out, int cid, int lac)
        throws IOException
{
    DataOutputStream dataOutputStream = new DataOutputStream(out);
    dataOutputStream.writeShort(21);
    dataOutputStream.writeLong(0);
    dataOutputStream.writeUTF("en");
    dataOutputStream.writeUTF("Android");
    dataOutputStream.writeUTF("1.0");
    dataOutputStream.writeUTF("Web");
    dataOutputStream.writeByte(27);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(3);
    dataOutputStream.writeUTF("");

    dataOutputStream.writeInt(cid);
    dataOutputStream.writeInt(lac);

    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.flush();
}
}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinparashar.celldemo">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".CellActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

2 个答案:

答案 0 :(得分:0)

看起来您正在尝试在UI线程上发出网络请求 - 我猜测这是您正在查看的例外情况。尝试使用AsyncTask或类似方法将RqsLocation调用移动到后台线程。

答案 1 :(得分:0)

即使我有同样的问题,但也有例外情况“ java.io.IOException:不允许对www.google.com的明文HTTP通信

如果您使用的是Android 9 Pie,则必须在清单应用程序中设置networkSecurityConfig。

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
     <application android:networkSecurityConfig="@xml/network_security_config">
     </application>
</manifest>

在res / xml / network_security_config.xml中创建文件,并添加以下代码。

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>