2013-04-07 75 views
1

我正在嘗試連接到一個URL以獲取geopoints在我的android應用程序的mapview中繪製路線。但是,當我嘗試連接到URL以獲取點時,出現此錯誤。HttpURLConnection打開跟蹤文件時出錯:沒有這樣的文件或目錄

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" /> 

我在Eclipse 4.2.2在Mac OSX獅子使用模擬器運行此。

感謝您的幫助!

編輯

對不起,我離開了一些重要的信息,這裏有我的所有權限在我的清單

<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" /> 

EDIT 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) 
+0

你能否檢查你是否真的從該URL中獲得kml,因爲我從中獲取了HTML。 也發佈你正在得到的錯誤。 – 2013-04-07 00:23:31

+0

添加完整的錯誤。我也注意到了這個html,但我想先弄錯這個錯誤。謝謝。 – 2013-04-07 00:30:14

+0

我編輯了答案。 – 2013-04-07 00:34:32

回答

2

當您試圖在主應用程序線程上訪問Internet時,您會收到此異常。您需要在不同的線程中完成所有網絡活動。您可以使用AsyncTask來完成此操作。

這樣做是爲了讓應用程序保持響應,即使阻塞了對網絡的呼叫。您可能還需要進一步閱讀here

+0

謝謝,但我很笨拙的離開了那裏,我已經有了。我已經更新了我的問題,對不起,因爲我離開了。 – 2013-04-07 00:19:19

+0

很酷,很有效。謝謝!至於我的問題與kml,它似乎谷歌刪除此功能,並找到了解決辦法在這裏http://stackoverflow.com/questions/11745314/why-retrieving-google-directions-for-android-using-kml-data-is -not-working-anymo/11745316#11745316萬一任何人發現這個問題,並陷入這個問題以及。 – 2013-04-07 03:49:59

相關問題