Java 在用户界面中多次按下按钮

Java 在用户界面中多次按下按钮,java,android,Java,Android,我有一个执行异步任务HTTP Get请求的按钮。但是,多次快速按下此按钮通常会将按钮UI冻结在按下状态—它使用XML选择器,我不知道为什么。我认为异步任务不会影响UI。有人能解释一下为什么按钮有时会冻结吗 我试过execute和executeOnExecutor,没什么区别 OnClick侦听器: 以及AsyncTask类本身: public class HTTPRequest extends AsyncTask<String, Void, Void> { private s

我有一个执行异步任务HTTP Get请求的按钮。但是,多次快速按下此按钮通常会将按钮UI冻结在按下状态—它使用XML选择器,我不知道为什么。我认为异步任务不会影响UI。有人能解释一下为什么按钮有时会冻结吗

我试过execute和executeOnExecutor,没什么区别

OnClick侦听器:

以及AsyncTask类本身:

public class HTTPRequest extends AsyncTask<String, Void, Void> {
    private static final int CONNECTION_TIMEOUT = 3000;

    @Override
    protected Void doInBackground(String... params) {
        String stringUrl = params[0];
        URL myUrl;
        HttpURLConnection connection = null;

        try {
            myUrl = new URL(stringUrl);

            connection = (HttpURLConnection) myUrl.openConnection();

            connection.setRequestMethod("GET");
            connection.setConnectTimeout(CONNECTION_TIMEOUT);
            Log.d("Request", "Sending command");
            connection.connect();
            InputStream in = connection.getInputStream();
            in.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }
}
如报告中所述

如果计算需要完成,get方法将等待,然后检索其结果

删除get方法。因此,您的主线程正在等待后台任务的响应


如果要处理AsyncTask类的结果,最好重写OnPostExecuteSult结果。它在doInBackgroundParams之后运行。。。参数与结果一致。

这是一个冻结状态,因为您的调用get将返回一个结果,并在需要时等待响应。啊,谢谢!我完全错过了我正在使用的。快。
public class HTTPRequest extends AsyncTask<String, Void, Void> {
    private static final int CONNECTION_TIMEOUT = 3000;

    @Override
    protected Void doInBackground(String... params) {
        String stringUrl = params[0];
        URL myUrl;
        HttpURLConnection connection = null;

        try {
            myUrl = new URL(stringUrl);

            connection = (HttpURLConnection) myUrl.openConnection();

            connection.setRequestMethod("GET");
            connection.setConnectTimeout(CONNECTION_TIMEOUT);
            Log.d("Request", "Sending command");
            connection.connect();
            InputStream in = connection.getInputStream();
            in.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }
}