空指针异常请求url gmaps v2 android
我正在做一个追踪两个地址之间路线的应用程序,这样lat lng就可以完美地工作空指针异常请求url gmaps v2 android,android,google-maps-android-api-2,Android,Google Maps Android Api 2,我正在做一个追踪两个地址之间路线的应用程序,这样lat lng就可以完美地工作 String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + start.latitude + "," + start.longitude + "&destination=" + end.latitude + "," + end.longit
String url = "http://maps.googleapis.com/maps/api/directions/xml?"
+ "origin=" + start.latitude + "," + start.longitude
+ "&destination=" + end.latitude + "," + end.longitude
+ "&sensor=false&units=metric&mode=driving";
现在我从用户那里得到输入的来源和目的地,到目前为止还可以。
我正在调试代码和url,以路由在浏览器中成功挂载的呼叫。但Java Null Point Exception中的错误代码正好发生在第行:
HttpPost httpPost = new HttpPost(url);
这是完整的代码,不明白如果url是有效的,为什么会出现空指针异常。谁能帮我解决我不知道该怎么办
public class GMapV2Direction {
public final static String MODE_DRIVING = "driving";
public final static String MODE_WALKING = "walking";
public GMapV2Direction() { }
public Document getDocument(String origin, String destination, String mode) {
String url = "http://maps.googleapis.com/maps/api/directions/xml?"
+ "origin=" + origin
+ "&destination=" + destination
+ "&sensor=false&units=metric&mode=driving";
Log.d("GoogleMapsDirection", url);
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getDurationText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DurationText", node2.getTextContent());
return node2.getTextContent();
}
public int getDurationValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DurationValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}
public String getDistanceText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DistanceText", node2.getTextContent());
return node2.getTextContent();
}
public int getDistanceValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(nl1.getLength() - 1);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DistanceValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}
public String getStartAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("start_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}
public String getEndAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("end_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}
public String getCopyRights (Document doc) {
NodeList nl1 = doc.getElementsByTagName("copyrights");
Node node1 = nl1.item(0);
Log.i("CopyRights", node1.getTextContent());
return node1.getTextContent();
}
public ArrayList<LatLng> getDirection (Document doc) {
NodeList nl1, nl2, nl3;
ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
nl1 = doc.getElementsByTagName("step");
if (nl1.getLength() > 0) {
for (int i = 0; i < nl1.getLength(); i++) {
Node node1 = nl1.item(i);
nl2 = node1.getChildNodes();
Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
nl3 = locationNode.getChildNodes();
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
double lat = Double.parseDouble(latNode.getTextContent());
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
double lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "points"));
ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
for(int j = 0 ; j < arr.size() ; j++) {
listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
}
locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "lat"));
lat = Double.parseDouble(latNode.getTextContent());
lngNode = nl3.item(getNodeIndex(nl3, "lng"));
lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
}
}
return listGeopoints;
}
private int getNodeIndex(NodeList nl, String nodename) {
for(int i = 0 ; i < nl.getLength() ; i++) {
if(nl.item(i).getNodeName().equals(nodename))
return i;
}
return -1;
}
private ArrayList<LatLng> decodePoly(String encoded) {
ArrayList<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
poly.add(position);
}
return poly;
}
我的be UI线程正在等待Http执行。 使用Asynctask for HttpClient为HttpClient设置一个重试超时,命令如下
HttpParams httpParameters = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(httpParameters);
client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
client.getParams().setParameter("http.socket.timeout", 2000);
client.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
httpParameters.setBooleanParameter("http.protocol.expect-continue", false);
HttpPost request = new HttpPost(url);
request.getParams().setParameter("http.socket.timeout", 5000);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("prm1", prm1value));
try {
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
...
..
HttpParams httpParameters=new BasicHttpParams();
HttpClient=新的默认HttpClient(httpParameters);
client.getParams().setParameter(“http.protocol.version”,HttpVersion.http_1_1);
client.getParams().setParameter(“http.socket.timeout”,2000);
client.getParams().setParameter(“http.protocol.content字符集”,http.UTF_8);
httpParameters.setBooleanParameter(“http.protocol.expect continue”,false);
HttpPost请求=新的HttpPost(url);
request.getParams().setParameter(“http.socket.timeout”,5000);
List postParameters=new ArrayList();
添加(新的BasicNameValuePair(“prm1”,prm1value));
试一试{
UrlEncodedFormEntity formEntity=新的UrlEncodedFormEntity(后参数,HTTP.UTF_8);
请求。setEntity(formEntity);
HttpResponse response=client.execute(请求);
...
..
对我来说,最快的解决方案是从用户输入中对地址簿的起点和终点进行地理编码,并捕获纬度和经度,以查询路线的工作方式
/*
* Geocodifica os endereços de origem e destino digitados pelo usuário
*/
Geocoder coder = new Geocoder(this, Locale.US);
List<Address> resultsOrigin = null;
List<Address> resultDestination = null;
try{
try {
resultsOrigin = coder.getFromLocationName(origin, 1);
resultDestination = coder.getFromLocationName(destination, 1);
} catch (IOException e) {
e.printStackTrace();
}if(resultsOrigin != null){
Address locationOrigin = resultsOrigin.get(0);
fromPosition = new LatLng(locationOrigin.getLatitude(), locationOrigin.getLongitude());
Address locationDestination = resultDestination.get(0);
toPosition = new LatLng(locationDestination.getLatitude(), locationDestination.getLongitude());
}
}catch (IllegalArgumentException i) {
i.printStackTrace();
}
/*
*地理编码:原始数据和数字数据的地理编码(Geocardicca os endereços de origem e destino digitados pelo usuário)
*/
Geocoder coder=新的Geocoder(this,Locale.US);
List resultsOrigin=null;
List resultDestination=null;
试一试{
试一试{
resultsOrigin=coder.getFromLocationName(来源,1);
resultDestination=coder.getFromLocationName(目的地,1);
}捕获(IOE异常){
e、 printStackTrace();
}if(resultsOrigin!=null){
地址locationOrigin=resultsOrigin.get(0);
fromPosition=new LatLng(locationOrigin.getLatitude(),locationOrigin.getLongitude());
地址locationDestination=resultDestination.get(0);
toPosition=new LatLng(locationDestination.getLatitude(),locationDestination.getLongitude());
}
}捕获(IllegalArgumentException i){
i、 printStackTrace();
}
你能发布堆栈跟踪吗?Rajesh我用堆栈跟踪编辑了这个问题。你有一个运行时异常:无法启动activity ComponentInfo。你确定问题出在google上吗?我相信是的,因为调试代码时错误发生在我HttpPost HttpPost=new HttpPost(url)的行上;但是url不是空的NPE在GMapV2Direction.java
的第69行。发布其中的内容。
/*
* Geocodifica os endereços de origem e destino digitados pelo usuário
*/
Geocoder coder = new Geocoder(this, Locale.US);
List<Address> resultsOrigin = null;
List<Address> resultDestination = null;
try{
try {
resultsOrigin = coder.getFromLocationName(origin, 1);
resultDestination = coder.getFromLocationName(destination, 1);
} catch (IOException e) {
e.printStackTrace();
}if(resultsOrigin != null){
Address locationOrigin = resultsOrigin.get(0);
fromPosition = new LatLng(locationOrigin.getLatitude(), locationOrigin.getLongitude());
Address locationDestination = resultDestination.get(0);
toPosition = new LatLng(locationDestination.getLatitude(), locationDestination.getLongitude());
}
}catch (IllegalArgumentException i) {
i.printStackTrace();
}