为什么我的 urlConnection.getContentLength 大小始终为 -1?
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为此传递正确的纬度和经度值。我可以用你的代码做到这一点。我尝试传递经度和纬度值,修正至小数点后 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