【问题标题】:Why is my urlConnection.getContentLength size always -1? [duplicate]为什么我的 urlConnection.getContentLength 大小总是-1? [复制]
【发布时间】:2011-06-06 11:39:06
【问题描述】:

可能重复:
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());

【问题讨论】:

标签: android parsing google-maps kml


【解决方案1】:

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

尝试以下。 浦那 => srcLat = 18.4577015; 浦那 => srcLong = 73.8552158;

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

结果:解析前大小:47555

【讨论】:

  • 我试过你的但仍然得到解析大小:-1。知道有什么问题吗?
  • 如果您尝试使用我的值,那么我们应该得到相同的 URL。我已经添加了上面的 URL,请在您最后尝试比较您的代码正在准备的 URL。
猜你喜欢
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
相关资源
最近更新 更多