我正在尝试向Google寻求两个地理点之间的路径。但我有nullpointerexception。
在这里,我称之为制作小说的方法:
LatLng start = new LatLng(13.6871, 100.5352);
LatLng end = new LatLng(13.6836, 100.5390);
StringBuilder SBdoc = null;
try {
SBdoc = getDocument(start, end, MODE_DRIVING);
if (SBdoc != null){
String doc = SBdoc.toString();
}else {Log.e("The route of the line is null","");}
} catch (IOException e) {
e.printStackTrace();
}
这是方法:
public StringBuilder getDocument(LatLng start, LatLng end, String mode) throws IOException {
String url = "http://maps.googleapis.com/maps/api/directions/json?"
+ "origin=" + start.latitude + "," + start.longitude
+ "&destination=" + end.latitude + "," + end.longitude
+ "&sensor=false&units=metric&mode=driving";
StringBuilder builder = null;
URL categoriesWSUrl = new URL(url);
URLConnection connection = categoriesWSUrl.openConnection();
String line;
builder = new StringBuilder();
InputStream response = connection.getInputStream();
InputStreamReader isr = new InputStreamReader( response );
BufferedReader reader = new BufferedReader(isr);
while((line = reader.readLine()) != null){
builder.append(line + "\n");
}
return builder;
}
logcat的stractrace:
05-04 14:29:43.550: E/AndroidRuntime(11689): FATAL EXCEPTION: main
05-04 14:29:43.550: E/AndroidRuntime(11689): java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.Map}: android.os.NetworkOnMainThreadException
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.os.Looper.loop(Looper.java:137)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-04 14:29:43.550: E/AndroidRuntime(11689): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 14:29:43.550: E/AndroidRuntime(11689): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 14:29:43.550: E/AndroidRuntime(11689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-04 14:29:43.550: E/AndroidRuntime(11689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-04 14:29:43.550: E/AndroidRuntime(11689): at dalvik.system.NativeStart.main(Native Method)
05-04 14:29:43.550: E/AndroidRuntime(11689): Caused by: android.os.NetworkOnMainThreadException
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-04 14:29:43.550: E/AndroidRuntime(11689): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-04 14:29:43.550: E/AndroidRuntime(11689): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-04 14:29:43.550: E/AndroidRuntime(11689): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
05-04 14:29:43.550: E/AndroidRuntime(11689): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-04 14:29:43.550: E/AndroidRuntime(11689): at package.Map.getDocument(Map.java:110)
05-04 14:29:43.550: E/AndroidRuntime(11689): at package.Map.onCreate(Map.java:84)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.Activity.performCreate(Activity.java:5104)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-04 14:29:43.550: E/AndroidRuntime(11689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
答案 0 :(得分:1)
NetworkOnMainThreadException
此错误,因为您必须在后台线程中运行一些与网络相关的操作。使用asynctask
并在doInBackground()
中编写代码。
答案 1 :(得分:0)
您还应该将AsyncTask用于该方法“getDocument”,请参阅AsyncTask
答案 2 :(得分:0)
你应该使用asynctask。如果在主UI线程上运行与网络相关的操作,则会收到NetworkOnMainThreadException。这适用于HoneyComb及更高版本。所以使用asynctask
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
对于asynctask,请检查以下链接
http://developer.android.com/reference/android/os/AsyncTask.html
在doInBackground()
中调用你的getDocument(params)注意:不要从doInbackground()
更新ui不要忘记阅读标题为“4个步骤”的主题。