Android网络请求引发异常

时间:2014-08-11 15:31:20

标签: java android eclipse

我已将来自SO的代码拼凑在一起,我正试图在Eclipse的Android项目中使用Java执行Web请求。

我的代码在try / catch块中,我不熟悉Eclipse。我设置了断点,但执行从未停止过。

这是我的代码:

    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();
    }

我已将此添加到我的清单

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

我错过了什么?

编辑: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)

1 个答案:

答案 0 :(得分:1)

您无法在应用程序的主线程中执行网络操作。创建一个AsyncTask并在doInBackground方法中运行代码。

从活动中,您可以像这样运行AsyncTask:

new AsyncTaskClassName.execute(params);