简体   繁体   English

Java Apache httpclient阻止GUI

[英]Java Apache httpclient blocking GUI

I am currently developing an HTTP application using Apache's http API and I am using a GUI. 我目前正在使用Apache的http API开发HTTP应用程序,并且正在使用GUI。 After after every GET or POST request I want to update a GUI TextArea with some messages. 在每个GET或POST请求之后,我想用一些消息更新GUI TextArea。 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. 我还注意到,如果在每次请求后在控制台上编写消息,都会显示该消息,但是如果我在GUI上编写,则所有消息都将显示在末尾。

Here is some code snippets: 以下是一些代码片段:

GUI constructor: GUI构造函数:

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

SetMessage class: 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);
    }

}

The GET request class (every request is made by a thread): 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
}

And HttpConnection Class (the instance of this class is created when I press a button on the GUI): 和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();

}

Oh! 哦! And the GUI's SetText method: 以及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");                        
        }
    }

Can anyone help me with this problem? 谁能帮助我解决这个问题? Thanks! 谢谢! } }

Yup, pretty standard behaviour for a GUI. 是的,GUI的标准行为。 You will need to do the HTTP requests in another thread, then notify the GUI thread to update the UI. 您将需要在另一个线程中执行HTTP请求,然后通知GUI线程更新UI。 Swing in particular demands that the UI is updated from a single thread, the event dispatching thread to be precise. 特别地,Swing要求从单个线程更新UI,准确地说,是事件分配线程。

See SwingUtilities#isEventDispatchThread() , SwingUtilities#invokeLater() and the SwingWorker class. 请参见SwingUtilities#isEventDispatchThread()SwingUtilities#invokeLater()SwingWorker类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM