我正在做一个跟踪两个地址之间路线的应用程序,所以使用lat lng它可以很好地工作
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;
}
}
已编辑:printStackTrace
11-12 15:27:27.459: E/AndroidRuntime(24478): FATAL EXCEPTION: main
11-12 15:27:27.459: E/AndroidRuntime(24478): Process: br.com.myapp, PID: 24478
11-12 15:27:27.459: E/AndroidRuntime(24478): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.myapp.Search}: java.lang.NullPointerException
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.os.Handler.dispatchMessage(Handler.java:102)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.os.Looper.loop(Looper.java:136)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-12 15:27:27.459: E/AndroidRuntime(24478): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 15:27:27.459: E/AndroidRuntime(24478): at java.lang.reflect.Method.invoke(Method.java:515)
11-12 15:27:27.459: E/AndroidRuntime(24478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-12 15:27:27.459: E/AndroidRuntime(24478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-12 15:27:27.459: E/AndroidRuntime(24478): at dalvik.system.NativeStart.main(Native Method)
11-12 15:27:27.459: E/AndroidRuntime(24478): Caused by: java.lang.NullPointerException
11-12 15:27:27.459: E/AndroidRuntime(24478): at br.com.myapp.GMapV2Direction.getDurationValue(GMapV2Direction.java:69)
11-12 15:27:27.459: E/AndroidRuntime(24478): at br.com.myapp.Search.onCreate(Search.java:59)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.Activity.performCreate(Activity.java:5231)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-12 15:27:27.459: E/AndroidRuntime(24478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-12 15:27:27.459: E/AndroidRuntime(24478): ... 11 more
答案 0 :(得分:0)
我的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);
...
..
答案 1 :(得分:0)
对我来说,最快的解决方案是从用户输入中对地理位置的起点和终点进行地理编码,并捕获纬度和经度以查询路径的工作方式。
/*
* 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();
}