Java Apache httpclient 阻塞 GUI

发布于 2024-12-01 02:28:29 字数 2117 浏览 1 评论 0原文

我目前正在使用 Apache 的 http API 开发 HTTP 应用程序,并且使用 GUI。在每次 GET 或 POST 请求之后,我想用一些消息更新 GUI TextArea。问题是这些消息是在所有请求完成后才出现的。

我还注意到,如果我在每次请求后在控制台上写消息,该消息就会出现,但如果我在 GUI 上写,所有消息都会出现在最后。

下面是一些代码片段:

GUI 构造函数:

public GUI() {
        initComponents();
        SetMessage.gui = this;
}

SetMessage 类:

public class SetMessage implements Runnable{

    public static GUI gui;
    private String msg;

    public SetMessage( String msg){
        synchronized(gui){
            this.msg = msg;
        }
    }

    public void run() {
        gui.setText(msg);
    }

}

GET 请求类(每个请求都是由一个线程发出):

public class SendGetReq extends Thread {

private HttpConnection hc = null;
private DefaultHttpClient httpclient = null;
private HttpGet getreq = null;
private int step = -1;
private String returnString = null;

public SendGetReq(HttpConnection hc, DefaultHttpClient httpclient, HttpGet getreq, int step) {
    this.hc = hc;
    this.httpclient = httpclient;
    this.getreq = getreq;
    this.step = step;
}

@Override
public void run() {
   // CODE
}

和 HttpConnection 类(当我在 GUI 上按下按钮时创建该类的实例):

    public class HttpConnection {
        private DefaultHttpClient httpclient = null;
        private HttpGet getreq = null;
        private HttpPost postreq = null;
private SendGetReq tempGet = null;
         // More fields
        private void RandomMethod(){
//Initialize getreq
(tempGet = new SendGetReq(this, httpclient, getreq, 0)).start();
new SetMessage("Message").run();

}

哦! GUI 的 SetText 方法:

public synchronized void setText(String msg){
        if(!"".equals(msg)){
            Date currentDate = new Date();
            Calendar calendar = GregorianCalendar.getInstance();
            calendar.setTime(currentDate);
            jTextArea1.append(calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+" --- "+msg+"\n");                        
        }
    }

任何人都可以帮我解决这个问题吗?谢谢! }

I am currently developing an HTTP application using Apache's http API and I am using a GUI. After after every GET or POST request I want to update a GUI TextArea with some messages. The problem is that those messages appear after all the requests are done.

I also noticed that if I write messages on the console after every request, the message appear, but if I write on the GUI all the messages appear on the end.

Here is some code snippets:

GUI constructor:

public GUI() {
        initComponents();
        SetMessage.gui = this;
}

SetMessage class:

public class SetMessage implements Runnable{

    public static GUI gui;
    private String msg;

    public SetMessage( String msg){
        synchronized(gui){
            this.msg = msg;
        }
    }

    public void run() {
        gui.setText(msg);
    }

}

The GET request class (every request is made by a thread):

public class SendGetReq extends Thread {

private HttpConnection hc = null;
private DefaultHttpClient httpclient = null;
private HttpGet getreq = null;
private int step = -1;
private String returnString = null;

public SendGetReq(HttpConnection hc, DefaultHttpClient httpclient, HttpGet getreq, int step) {
    this.hc = hc;
    this.httpclient = httpclient;
    this.getreq = getreq;
    this.step = step;
}

@Override
public void run() {
   // CODE
}

And HttpConnection Class (the instance of this class is created when I press a button on the GUI):

    public class HttpConnection {
        private DefaultHttpClient httpclient = null;
        private HttpGet getreq = null;
        private HttpPost postreq = null;
private SendGetReq tempGet = null;
         // More fields
        private void RandomMethod(){
//Initialize getreq
(tempGet = new SendGetReq(this, httpclient, getreq, 0)).start();
new SetMessage("Message").run();

}

Oh! And the GUI's SetText method:

public synchronized void setText(String msg){
        if(!"".equals(msg)){
            Date currentDate = new Date();
            Calendar calendar = GregorianCalendar.getInstance();
            calendar.setTime(currentDate);
            jTextArea1.append(calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+" --- "+msg+"\n");                        
        }
    }

Can anyone help me with this problem? Thanks!
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海里的那抹蓝 2024-12-08 02:28:29

是的,GUI 的相当标准的行为。您需要在另一个线程中执行 HTTP 请求,然后通知 GUI 线程更新 UI。 Swing 特别要求 UI 从单个线程(准确地说是事件调度线程)更新。

请参阅 SwingUtilities#isEventDispatchThread()SwingUtilities#invokeLater()SwingWorker 类。

Yup, pretty standard behaviour for a GUI. You will need to do the HTTP requests in another thread, then notify the GUI thread to update the UI. Swing in particular demands that the UI is updated from a single thread, the event dispatching thread to be precise.

See SwingUtilities#isEventDispatchThread(), SwingUtilities#invokeLater() and the SwingWorker class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文