Android:“致命异常”在使用谷歌地图时调用URLConnection上的getInputStream()

时间:2014-03-14 06:29:50

标签: java android eclipse google-maps android-activity

提前感谢您的帮助!

我将一个请求放到maps.googleapis(http://maps.googleapis.com/maps/api/directions/json?)并使用以下代码调用它

this.myURL = new URL(mapsUrl);
URLConnection conn = myUrl.openConnection();
InputStream in = new BufferedInputStream(conn.getInputStream());
return in;

导致这一行代码的链事件在MainActivity.java中的onPostResume中引发(我最初也尝试在onActivityResult中运行它,但是没有工作,所以我尝试了onPostResume,但两者都会导致同样的错误)。

错误消息在

下面提供
03-14 02:07:52.123: W/dalvikvm(28964): threadid=1: thread exiting with uncaught exception (group=0x41f88ba8)
03-14 02:07:52.223: E/AndroidRuntime(28964): FATAL EXCEPTION: main
03-14 02:07:52.223: E/AndroidRuntime(28964): Process: com.example.BTManager.local, PID: 28964
03-14 02:07:52.223: E/AndroidRuntime(28964): java.lang.RuntimeException: Unable to resume activity {com.example.BTManager.local/com.example.BTManager.local.MainActivity}: android.os.NetworkOnMainThreadException
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.os.Looper.loop(Looper.java:136)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at java.lang.reflect.Method.invokeNative(Native Method)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at java.lang.reflect.Method.invoke(Method.java:515)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at dalvik.system.NativeStart.main(Native Method)
03-14 02:07:52.223: E/AndroidRuntime(28964): Caused by: android.os.NetworkOnMainThreadException
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.directions.route.XMLParser.getInputStream(XMLParser.java:29)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.directions.route.GoogleParser.parse(GoogleParser.java:32)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at com.example.BTManager.local.MainActivity.onPostResume(MainActivity.java:630)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.app.Activity.performResume(Activity.java:5323)
03-14 02:07:52.223: E/AndroidRuntime(28964):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
03-14 02:07:52.223: E/AndroidRuntime(28964):    ... 10 more

1 个答案:

答案 0 :(得分:1)

您已尝试在主线程中加载数据

android.os.NetworkOnMainThreadException

您必须在某些工作线程中执行所有与网络相关的操作。

herehere学习。

感谢。