在尝试访问网页时,应用程序是FC

时间:2013-11-04 17:03:07

标签: java android androidhttpclient

我的应用程序中有以下代码:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.pagesbyz.com");
ResponseHandler<String> resHandler = new BasicResponseHandler();
try {
    String page = httpClient.execute(httpGet, resHandler);
    Toast.makeText(getApplicationContext(), "SUCCESS", 2000).show();
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我将以下行添加到我的清单中:

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

我的应用FC,出现以下LogCat错误:

11-04 11:59:14.137: E/AndroidRuntime(11052): FATAL EXCEPTION: main
11-04 11:59:14.137: E/AndroidRuntime(11052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testing/com.test.testing.AndroidFragmentation}: android.os.NetworkOnMainThreadException
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.os.Looper.loop(Looper.java:137)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.main(ActivityThread.java:5195)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at dalvik.system.NativeStart.main(Native Method)
11-04 11:59:14.137: E/AndroidRuntime(11052): Caused by: android.os.NetworkOnMainThreadException
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at com.test.testing.AndroidFragmentation.onCreate(AndroidFragmentation.java:36)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.Activity.performCreate(Activity.java:5104)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)

知道怎么解决吗?谢谢!

4 个答案:

答案 0 :(得分:3)

这解释为here。您应该使用AsyncTask进行网络访问,或使用Thread / HandlerThread。自API 11以来,禁止在主(UI)线程上进行网络访问。

你可以尝试这样的事情:

private class HttpTask extends AsyncTask<String, Void, Void>{
    @Override
    protected Void doInBackground(String... params) {
        String url = params[0];
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        ResponseHandler<String> resHandler = new BasicResponseHandler();
        try {
            String page = httpClient.execute(httpGet, resHandler);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Toast.makeText(getApplicationContext(), "SUCCESS", 2000).show();
    }
}

并从您的活动/片段

HttpTask task = new HttpTask();
task.execute(url);

答案 1 :(得分:2)

您必须从nonUI线程发出请求,例如使用AsyncTask或?扩展线程

答案 2 :(得分:1)

您遇到的问题是您正在主线程上运行网络代码,因为NetworkOnMainThreadException指出。

查看这个问题的答案,了解如何使用AsyncTask来避免这种情况:How to fix android.os.NetworkOnMainThreadException?

此博客文章对异常进行了深入解释: http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

最后,官方的NetworkOnMainThreadException文档可以在这里找到:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

答案 3 :(得分:1)

您正试图直接从活动中获取服务器的结果。这允许直到2.x.从Honeycomb开始,NetworkOnMainThreadException被引入。由于此操作可能需要很长时间,因此它将阻止UI线程,并且可能会显示ANR。对于任何时间进行操作,应使用单独的线程。

您可以使用AsyncTask。它在您的活动的单独线程上运行。

在AsyncTask的doInBackground()中,您可以执行所需的操作并将其结果传递给此类的onPostExecute(),您可以根据该结果显示要在应用程序中显示的Toast消息。