Android 当试图从GPS获取位置时,一部手机中的应用程序强制关闭,但在另一部手机中它可以工作

Android 当试图从GPS获取位置时,一部手机中的应用程序强制关闭,但在另一部手机中它可以工作,android,get,gps,location,Android,Get,Gps,Location,我试图通过gps获取地址,将坐标转换为我的位置。我正在使用以下代码: latitude1 = (location.getLatitude()); longitude2= (location.getLongitude()); JSONObject ret = getLocationInfo(); JSONObject location2; String location_string; try {

我试图通过gps获取地址,将坐标转换为我的位置。我正在使用以下代码:

latitude1 = (location.getLatitude());
        longitude2= (location.getLongitude());

        JSONObject ret = getLocationInfo(); 
        JSONObject location2;
        String location_string;
        try {
            location2 = ret.getJSONArray("results").getJSONObject(0);
            location_string = location2.getString("formatted_address");
            Log.d("test", "formattted address:" + location_string);
            StringRua = (" " + location_string);
        } catch (JSONException e1) {
            e1.printStackTrace();

        }

它在我的手机里很好用。但是,当我的朋友尝试使用相同的应用程序时,当这部分代码被执行时,应用程序只是强制关闭

请记住,我已经尝试使用:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude1, longitude2, 1);
我使用,来调用异步:

  new AsyncCaller().execute();
“StringRua=(“”+location_string);”未获取任何位置

编辑2:我将公共JSONObject getLocationInfo()放在私有类AsyncCaller Extendes AsyncTask中 {

我只是发现它在第一次调用asynctask时并没有得到位置,但在第二次调用asynctask时它就工作了。但是我需要让它在第一次调用asynctask时工作。
OBS:在我朋友的手机中,它不再崩溃,但他总是得到空值

问题是,我猜主界面上的网络I/O,你需要一个
异步任务
来执行主线程中的网络操作

有关
AsyncTasks

从Android Honeycom及更高版本开始,应用程序将因
NetworkOnMainThreadException
而崩溃,但将在早期版本上运行

来自开发者参考

This is only thrown for applications targeting the Honeycomb SDK or higher. 
Applications targeting earlier SDK versions are allowed to do networking on 
their main event loop threads, but it's heavily discouraged.

你能把logcat和规格贴在设备上吗?我不能:/。我现在不在设备上,我正在给他发.apk到他家里测试。logcat在这种情况下帮不了忙,因为我使用的是gps定位仪和加速度计,我无法在AVD中模拟(我试过了,但没有成功),所以我只能在手机上测试我的应用程序,只有在手机上,应用程序才会进入执行上述命令的“如果”。你和你朋友的手机上的android版本是什么?我的版本是2.3.5姜饼(与应用程序一起工作的手机)我朋友的电话是4.1.2 Jelly BeanI我把代码改成了异步任务,但现在我有另一个问题。我用它更新了我的帖子。@user2757741接受这个答案并问另一个有相关细节的问题,以便其他人可以帮助你。:)
public JSONObject getLocationInfo() {

    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+latitude1+","+longitude2+"&sensor=true");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
        } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonObject;
}
  new AsyncCaller().execute();
This is only thrown for applications targeting the Honeycomb SDK or higher. 
Applications targeting earlier SDK versions are allowed to do networking on 
their main event loop threads, but it's heavily discouraged.