Android 主线程上的HttpClient查询在处于可运行状态时持续崩溃

Android 主线程上的HttpClient查询在处于可运行状态时持续崩溃,android,android-asynctask,httpclient,runnable,Android,Android Asynctask,Httpclient,Runnable,我已经体验过这段代码在Android 2.2上运行得很顺利。但在安卓4.0上它崩溃了 我假设这是由HttpClient引起的。因此,我将代码移动到一个可运行的,但它一直崩溃 new Runnable() { @Override public void run() { try { HttpClient client = new DefaultHttpClient();

我已经体验过这段代码在Android 2.2上运行得很顺利。但在安卓4.0上它崩溃了

我假设这是由HttpClient引起的。因此,我将代码移动到一个可运行的,但它一直崩溃

new Runnable() {

        @Override
        public void run() {
            try {        
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(serverroot + URI_ARGS));
                client.execute(request);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.run();
有没有其他方法不使用AsyncTask就可以做到这一点?

从新的异常
NetworkOnMainThreadException
开始,如果您在UI线程上使用网络,将抛出该异常,因此您需要将代码移出UI线程
Runnable
只是一个界面,如果没有真正的
Thread
,它将无法帮助您

new Thread(new Runnable() {

    @Override
    public void run() {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(serverroot + URI_ARGS));
            client.execute(request);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

你能提供这次会议的记录吗error@MatoKormuth实际上,您需要启动新线程,它将在UI线程外执行操作。如果您不想与UI交互,可以使用此代码,否则请使用
AsyncTask
,它还可以创建单独的线程,并为您提供与UI交互的方便方式。