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

发布于 2024-11-14 00:03:01 字数 1937 浏览 2 评论 0原文

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

Possible Duplicate:
getContentLength() returning -1 on some devices and not others

I'm doing on an Android app that will show the the route from a place to another place.
So firstly I need to get the kml file and then parse in. However, my parse size is -1 which is not suppose to be so. The parse size must be bigger than -1 but I couldn't get it. Can anyone help me out? Thank you!

// 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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-11-21 00:03:01

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

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

孟买 =>目的地纬度 = 19.017576;
孟买 =>目标长 = 72.8562427;

结果:解析前大小:47555

You need to pass the correct latitude and longitude values for this. I could do this with your code. I tried passing Longitude and latitude values corrected up to 7 decimal points. I tried this.

Try with following.
Pune = > srcLat = 18.4577015;
Pune = > srcLong = 73.8552158;

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

Result: Before parse size:47555

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文