1

我正在尝试连接到一个 url 以获取地理点以在我的 android 应用程序的地图视图中绘制路线。但是,当我尝试连接到 url 以获取积分时出现此错误。

URL url = new URL("http://maps.google.com/maps?f=d&hl=en&saddr=59.690289,16.60868&daddr=59.6,16.60868&ie=UTF8&0&om=0&output=kml");
Log.d("log","url: " + url);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();          urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
Log.d("log","connected");

我的清单中有以下内容

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我正在使用模拟器在 Mac OSX Lion 上的 eclipse 4.2.2 中运行它。

谢谢你的帮助!

编辑

抱歉,我遗漏了一些重要信息,这是我清单中的所有权限

<uses-permission android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

编辑 2

这是完整的错误

04-07 00:24:46.842: E/AndroidRuntime(7280): FATAL EXCEPTION: main
04-07 00:24:46.842: E/AndroidRuntime(7280): java.lang.RuntimeException: Unable to start activity ComponentInfo{markustenghamn.sfsfum/markustenghamn.sfsfum.MapHandlerActivity}: android.os.NetworkOnMainThreadException
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.os.Looper.loop(Looper.java:137)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at java.lang.reflect.Method.invoke(Method.java:511)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at dalvik.system.NativeStart.main(Native Method)
04-07 00:24:46.842: E/AndroidRuntime(7280): Caused by: android.os.NetworkOnMainThreadException
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at markustenghamn.sfsfum.MapHandlerActivity.DrawPath(MapHandlerActivity.java:166)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at markustenghamn.sfsfum.MapHandlerActivity.callMap(MapHandlerActivity.java:126)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at markustenghamn.sfsfum.MapHandlerActivity.onCreate(MapHandlerActivity.java:89)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.Activity.performCreate(Activity.java:5104)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-07 00:24:46.842: E/AndroidRuntime(7280):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-07 00:24:46.842: E/AndroidRuntime(7280):     ... 11 more
04-07 00:24:49.343: E/Trace(7297): error opening trace file: No such file or directory (2)
4

1 回答 1

2

当您尝试在主应用程序线程上访问 Internet 时,您会遇到此异常。您需要在不同的线程中执行所有网络活动。您可以使用AsyncTask来完成此操作。

这样做是为了即使有对网络的阻塞调用,应用程序也会保持响应。您可能还想在此处进一步阅读。

于 2013-04-07T00:09:19.550 回答