2012-11-26 35 views
0

所以IAM使用反向地理編碼,一切工作正常,但我收到包裝在StringBuilder的解析XML數據中的StringBuilder

我期待得到的只是一號線如XML格式的響應。 :我想item1中的一個字符串,如何實現這一點?

這是我的代碼;

public void getPlaces() { 

HttpURLConnection connection = null; 
URL serverAddress = null; 
Double latitude = 37.422006; 
Double longitude = -122.084095; 

try { 
    // build the URL using the latitude & longitude you want to lookup 
    // NOTE: I chose XML return format here but you can choose something else 
    serverAddress = new URL("http://maps.google.com/maps/geo?q=" + Double.toString(latitude) + "," + Double.toString(longitude) + 
       "&output=xml&oe=utf8&sensor=true"); 
    //set up out communications stuff 
    connection = null; 

    //Set up the initial connection 
    connection = (HttpURLConnection)serverAddress.openConnection(); 
    connection.setRequestMethod("GET"); 
    connection.setDoOutput(true); 
    connection.setReadTimeout(10000); 

    connection.connect(); 

    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 

    while ((line = rd.readLine()) != null) 
    { 
     sb.append(line + '\n'); 
    } 
    // now the response is packaged in a string, 
    // parse the XML or whatever you need 
    String responseText = sb.toString(); 
    Toast.makeText(this, responseText, Toast.LENGTH_LONG).show(); 
} catch (Exception ex) { 
    ex.printStackTrace(); 
} 



} 

謝謝。

編輯: 解決使用此鏈接:

http://www.smnirven.com/blog/2009/10/09/reverse-geocode-lookup-using-google-maps-api-on-android-revisited/

+0

變線拿到位置而塊這個sb.append(線); –

+0

我只想在XML數據上得到1-2項,你說我應該刪除\ n? – user1839514

回答

0

可以使用地理編碼

Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); 
addresses = geocoder.getFromLocation(lattitude,longitude, 1); 
+0

是的,我知道,但這是更好使用谷歌因爲GeoCoder始終給我空結果 – user1839514

+0

你使用哪個提供商?網絡或GPS提供商.. – Akhil

+0

我不知道,當我使用它我的wifi和gps被啓用,無論如何,我發佈的鏈接解決了我的問題,沒有問題 – user1839514