Android 为什么我的urlConnection.getContentLength大小始终为-1?
可能重复:Android 为什么我的urlConnection.getContentLength大小始终为-1?,android,parsing,google-maps,kml,Android,Parsing,Google Maps,Kml,可能重复: 我在一个Android应用程序上做这件事,它会显示从一个地方到另一个地方的路线。 所以首先我需要获取kml文件,然后解析进来。然而,我的解析大小是-1,这是不应该的。解析大小必须大于-1,但我无法获取它。有人能帮我吗?谢谢大家! // connect to map web service StringBuilder urlString = new StringBuilder(); urlString.append("http://maps.google.com/ma
我在一个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());
您需要为此传递正确的纬度和经度值。我可以用你的代码来做。我试着传递修正到7个小数点的经度和纬度值。我试过了 试着用下面的方法。 Pune=>srcLat=18.4577015; Pune=>srcLong=73.8552158 孟买=>Destrat=19.017576; 孟买=>destLong=72.8562427
结果:解析前大小:47555可能重复:我尝试了你的,但仍然得到解析大小:-1。你知道怎么回事吗?如果你尝试使用我的值,那么我们应该得到相同的URL。我已经在上面添加了URL,请在你的末尾尝试比较代码准备的URL。