2011-06-06 79 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