1

I am trying to connect to an url to get geopoints to draw a route in a mapview for my android app. However I get this error when I try to connect to the url to get the points.

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");

I have the following in my manifest

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

I am running this in eclipse 4.2.2 on Mac OSX Lion using the emulator.

Thanks for your help!

EDIT

Sorry, I left out some important info, here are all my permissions in my manifest

<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

here is the full error

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)
aphex
  • 3,372
  • 2
  • 28
  • 56
Uberswe
  • 1,038
  • 2
  • 16
  • 36
  • Can you check whether you are really getting kml out of that URL, because I get HTML out of it. Also post the error that you are getting. – Pradeep Pati Apr 07 '13 at 00:23
  • Full error added. I also noticed the html but I wanted to get this error out of the way first. Thanks. – Uberswe Apr 07 '13 at 00:30

1 Answers1

2

As you are trying to access Internet on the main Application thread, you are getting this exception. You need to do all network activity in a different thread. You can use AsyncTask to accomplish this.

This is done so the the application will stay responsive even if there are blocking calls to the network. You may also want to read further here.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
  • Thanks, but I was dumb to leave that out, I already have that. I have updated my question, sorry for leaving that out. – Uberswe Apr 07 '13 at 00:19
  • Cool, that works. Thanks! As for my issue with the kml, it seems google removed this feature and found the solution here http://stackoverflow.com/questions/11745314/why-retrieving-google-directions-for-android-using-kml-data-is-not-working-anymo/11745316#11745316 in case anyone else finds this question and gets stuck on that issue as well. – Uberswe Apr 07 '13 at 03:49