【问题标题】:when calling the geocoder constructor in a thread the connection crashes在线程中调用地理编码器构造函数时,连接崩溃
【发布时间】:2011-05-29 14:53:23
【问题描述】:

我几乎没有尝试在 Android 中的 Runnable 类中实现地理编码器: 我就是这样做的:

Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);


                List<Address> list = geocoder.getFromLocation(
                        (int)(coord.getLat()), (int)(coord.getLon()), 1);

                if (list != null && list.size()>0 ){

                    Address address=list.get(0);
                     String result = address.getAddressLine(0) + ", " + address.getLocality();

                }

我的类,Runnable 实际上是一个线程,它连接到远程服务器并使用缓冲区从那里接收数据。

这是我的问题:

在我将带有地理编码器的线路放入我的代码之前,与服务器的连接一直很好。

现在,一旦我添加了地理编码器线,我与服务器的连接就崩溃了!!!

现在我确定这是因为地理编码器,因为直到今天我才将它添加到我的应用程序中,直到现在我的应用程序运行良好并且连接很牢固......

这是我的全部代码,如果有人能告诉我如何以更好的方式实现 Geocoder,请这样做:

public class ClientThread_special implements Runnable {

    int serverPort = 6500;
    private String serverIpAddress = "10.0.2.2";
    Coordinate coord;
    ObjectInputStream is;
    boolean stop = false;
    DBAdapter db;
    boolean t = false;
    int id;
    Context context;
    PrintStream strReturnedAddress;

    public ClientThread_special(DBAdapter db, Context context) {
        this.db = db;
        this.context = context;

    }

    public void run() {

        Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);


        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Socket socket = new Socket(serverIpAddress, serverPort);

            is = new ObjectInputStream(socket.getInputStream());

            try {
                while (!stop) {
                    coord = (Coordinate) is.readObject();
                    System.out.println("Date de la clientul de monitorizare:"
                            + coord.getLat());

                    List<Address> list = geocoder.getFromLocation(
                            (int)(coord.getLat()), (int)(coord.getLon()), 1);

                    if (list != null && list.size()>0 ){

                        Address address=list.get(0);
                         String result = address.getAddressLine(0) + ", " + address.getLocality();

                    }
                    System.out.println("adresa returnata folosind geocoder:"
                            + strReturnedAddress);

.................................................. .....

                }

            } catch (Exception e) {

                e.printStackTrace();

            }


            is.close();

        } catch (UnknownHostException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();
        }

    }

public void stop() {

        this.stop = true;

    }

}

我正在从服务器读取一些对象....如果需要其他更多详细信息,我在这里

【问题讨论】:

    标签: java android multithreading sockets object


    【解决方案1】:

    尝试将Log.v("EXCEPTION","message",e); 放入您的IO try/catch 块中,因为大多数情况下geocoder 失败时会抛出IOException。这可能会对您有所帮助,因为您会知道到底出了什么问题。我的感觉是地理编码器有时无法返回对应于纬度/经度的地址,因此请检查您的纬度/经度并检查列表是否为 NOT NULL,然后继续以防止崩溃。像这样 ....if (list.size()&gt;0) address=address_found ; else address="unknow address" ; and proceed like this,

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多