靠近在一个蜂窝电话应用力当试图从GPS得到的位置,但在另一个它的工作原理 [英] Application force close in one cell phone when trying to get location from GPS, but in the other one it works

查看:252
本文介绍了靠近在一个蜂窝电话应用力当试图从GPS得到的位置,但在另一个它的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去得到一个ADRESS低谷的全球定位系统,转换坐标我的位置。我用这code:

Im trying to get an adress trough the gps, converting the coordinates to my location. I am using this code:

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();

        }

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;
}

这在我的celphone工作正常。但是,当我的朋友尝试时,code的这一部分执行的应用程序使用相同的应用程序只是强制关闭。

It works fine in my celphone. But when my friend tries to use the same app when this part of the code is executed the app just force close.

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

Keep in mind that i already tried to use:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude1, longitude2, 1);

但它是执行此命令,实际上只是太慢了,它总是返回null。

But it was just too slow to execute this command and,in fact, it always returned null.

谁能帮我?
谢谢

Can anyone help me? Thank You

编辑:
我只是改变了我的code使用AsyncTask的,但现在我没有得到正确的位置,它只是说空。

I just changed my code to use asynctask, but now i am not getting the location correctly, it just says null.

下面是我做过什么:

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.show();
    }
    @Override
    protected Void doInBackground(Void... params) {

        //this method will be running on background thread so don't update UI frome here
        //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here


        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();

        }

        return null;
    }

异步调用的任务,这是AsyncTask的上面:

The async task calls, which is above the AsyncTask:

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();

在StringRua =(+ location_string);没有得到任何地点

The " StringRua = (" " + location_string); " is not getting any location

编辑2:我已经把公开的JSONObject getLocationInfo()内部私人的类AsyncCaller扩展的AsyncTask
{

EDIT 2 : I've put the public JSONObject getLocationInfo() inside private class AsyncCaller extends AsyncTask {

我刚刚发现,它没有得到位置在第一时间它调用的AsyncTask,但第二次它的作品。但我需要做这项工作的第一次打电话的AsyncTask。
OBS:在我的朋友打电话给它不崩溃了,但他总是让空

I just find out that it doesnt get the location the first time that it calls asynctask, but the second time it works. But i need to make this work the first time it call asynctask. OBS: In my friends phone it doesnt crash anymore but he is always getting Null

推荐答案

现在的问题是,我想网络上的主界面I / O,你需要一个的AsyncTask 执行在主线程的网络运营,

The problem is as I guess network I/O on main ui, you need an AsyncTask to perform the network operations in main thread,

有关 AsyncTasks

http://developer.android.com/reference/android/os/ AsyncTask.html

和Android蜂窝和上面的应用程序将 NetworkOnMainThreadException 崩溃,但在早期版本将工作。

and as of Android Honeycomb and above the application will crash with NetworkOnMainThreadException, but will work on earlier versions.

从开发者参考

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.

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

这篇关于靠近在一个蜂窝电话应用力当试图从GPS得到的位置,但在另一个它的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆