-1

I have pieced together code from SO and I am trying to perform a web request using Java in an Android project in Eclipse.

I have my code in a try/catch block and I am unfamiliar with Eclipse. I have set a breakpoint but execution never stops on it.

Here is my code:

    URLConnection connection = null;
    try {
        connection = new URL("http://itunes.apple.com/search?term=metallica").openConnection();
    } catch (MalformedURLException e2) {
        e2.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    BufferedReader reader = null;
    try {
    // this next line causes the exception
        reader = 
    new BufferedReader(new InputStreamReader(connection.getInputStream()), 1024 * 16);
    } catch (IOException e2) {
        e2.printStackTrace();
    } catch (Exception e3)
    {
        e3.printStackTrace();
    }

I have added this to my manifest

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

What am I missing?

Edit: Stacktrace

08-11 11:20:50.320: W/System.err(919): android.os.NetworkOnMainThreadException
08-11 11:20:50.340: W/System.err(919):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
08-11 11:20:50.350: W/System.err(919):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-11 11:20:50.350: W/System.err(919):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-11 11:20:50.350: W/System.err(919):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-11 11:20:50.350: W/System.err(919):  at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
08-11 11:20:50.360: W/System.err(919):  at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
08-11 11:20:50.360: W/System.err(919):  at werdna.app1.NewActivity.onCreate(ListHappyHoursActivity.java:47)
08-11 11:20:50.380: W/System.err(919):  at android.app.Activity.performCreate(Activity.java:5231)
08-11 11:20:50.380: W/System.err(919):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 11:20:50.380: W/System.err(919):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 11:20:50.380: W/System.err(919):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 11:20:50.380: W/System.err(919):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-11 11:20:50.400: W/System.err(919):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 11:20:50.400: W/System.err(919):  at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 11:20:50.400: W/System.err(919):  at android.os.Looper.loop(Looper.java:136)
08-11 11:20:50.410: W/System.err(919):  at android.app.ActivityThread.main(ActivityThread.java:5017)
08-11 11:20:50.410: W/System.err(919):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 11:20:50.410: W/System.err(919):  at java.lang.reflect.Method.invoke(Method.java:515)
08-11 11:20:50.410: W/System.err(919):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-11 11:20:50.430: W/System.err(919):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-11 11:20:50.430: W/System.err(919):  at dalvik.system.NativeStart.main(Native Method)
gaborsch
  • 15,408
  • 6
  • 37
  • 48
andrewb
  • 2,995
  • 7
  • 54
  • 95
  • Please provide the stack trace of the crash/exception in your logcat – WarrenFaith Aug 11 '14 at 15:35
  • It's because you are trying to access Internet from the main (display) thread. This is not authorized, you have to start a new thread for your net access. It is good practice anyway to do as little stuff as possible on the display thread, except of course display related tasks, it will make your application smoother. – Wildcopper Aug 11 '14 at 15:55

1 Answers1

1

You cannot perform a networking operation in the main thread of the application. Create an AsyncTask and run the code inside the doInBackground method.

From the activity, you can run the AsyncTask like this:

new AsyncTaskClassName.execute(params);
Goiko
  • 11
  • 2