为什么我的urlConnection.getContentLength大小始终为-1?

时间:2011-06-06 11:39:07

标签: android parsing google-maps kml

  

可能重复:
  getContentLength() returning -1 on some devices and not others

我正在使用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());

1 个答案:

答案 0 :(得分:0)

您需要为此传递正确的纬度和经度值。我可以用你的代码做到这一点。我尝试将经度和纬度值修正为最多7位小数。我试过了this

尝试以下操作。 Pune => srcLat = 18.4577015; Pune => srcLong = 73.8552158;

孟买=> destLat = 19.017576; 孟买=> destLong = 72.8562427;

结果:在解析大小之前:47555