2011-05-29 22 views
1

我想很難實現在一個Runnable類的android地理編碼: 這是我做的方式:在一個線程調用地理編碼器構造,當連接崩潰

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

       } 

我的課,可運行實際上是一個連接到遠程服務器並使用緩衝區從那裏接收數據的線程。

這裏是我的問題:

與服務器的連接或者是相當不錯,直到我把符合我的代碼中的地理編碼器。

現在,一旦我添加了地理編碼器線,我與服務器的連接崩潰了!

現在我知道肯定是因爲地理編碼器的,因爲只有今天我已經把它添加到我的應用程序,到現在爲止我的應用程序的工作非常出色,並連接呈強....

這裏是我的完整的代碼,如果有人可以告訴我怎樣才能以更好的方式實現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; 

    } 

} 

從服務器,我讀了一些對象....如果是neccesary其他進一步的細節我在這裏

回答

0

嘗試將Log.v("EXCEPTION","message",e);您的IO內try/catch塊作爲大多時候geocoder失敗拋出IOException。這可能會幫助你,因爲你會知道到底發生了什麼問題。我感覺地址解碼器有時無法返回對應於lat/long的地址,因此請檢查您的緯度/經度並檢查列表是否爲NOT NULL,然後繼續以避免崩潰。像這樣...... if (list.size()>0) address=address_found ; else address="unknow address" ; and proceed like this,

相關問題