2011-06-06 45 views
1

可能重複:
getContentLength() returning -1 on some devices and not others爲什麼我的urlConnection.getContentLength大小始終爲-1?

我針對Android應用程序,將顯示從一個地方到另一個地方的路線。 所以首先我需要獲取kml文件然後解析。但是,我的解析大小是-1,這不是假設的。解析大小必須大於-1,但我無法得到它。誰能幫我嗎?謝謝!

// connect to map web service 
    StringBuilder urlString = new StringBuilder(); 
    urlString.append("http://maps.google.com/maps?f=d&hl=en"); 
    urlString.append("&saddr=");//from 
    urlString.append(Double.toString((double)src.getLatitudeE6()/1.0E6)); 
    urlString.append(","); 
    urlString.append(Double.toString((double)src.getLongitudeE6()/1.0E6)); 
    urlString.append("&daddr=");//to 
    urlString.append(Double.toString((double)dest.getLatitudeE6()/1.0E6)); 
    urlString.append(","); 
    urlString.append(Double.toString((double)dest.getLongitudeE6()/1.0E6)); 
    urlString.append("&ie=UTF8&0&om=0&output=kml"); 
    Log.d("DrawPath","URL="+urlString.toString()); 

    // get the kml (XML) doc. And parse it to get the coordinates(direction route). 
    Document doc = null; 
    HttpURLConnection urlConnection= null; 
    URL url = null; 

    try 
    { 
     url = new URL(urlString.toString()); 
     urlConnection=(HttpURLConnection)url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.connect(); 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Log.d("Before parse", "size:" + urlConnection.getContentLength()); 
     doc = db.parse(urlConnection.getInputStream()); 
+0

可能重複:http://stackoverflow.com/questions/3617507/getcontentlength-returning-1-on-some-devices-and-not-others HTTP:// stackoverflow.com/questions/3418106/getcontentlength-return-1-only-in-wifi http://stackoverflow.com/questions/5428639/url-connection-returning-negative-value – 2011-06-06 11:43:35

回答

0

您需要爲此傳遞正確的經度和緯度值。我可以用你的代碼做到這一點。我試着將經度和緯度值修正爲7位小數點。我試過this

請嘗試以下操作。 Pune => srcLat = 18.4577015; Pune => srcLong = 73.8552158;

孟買=> destLat = 19.017576; Mumbai => destLong = 72.8562427;

結果:解析大小前:47555

+0

我試過你的,但仍然有解析大小: -1。任何想法有什麼不對? – Eric 2011-06-06 12:51:01

+0

如果你嘗試了我的價值觀,那麼我們應該得到相同的URL。我已經添加了上面的URL,嘗試在你的最後比較你的代碼正在準備的URL。 – sudmong 2011-06-06 13:59:15