2

我想使用进度窗格从此处返回响应,以便我可以显示加载对话框。代码工作正常,但有时我无法从这里得到响应。

代码片段是:

public class Connection
{

  StringBuffer messageBuffer = new StringBuffer();
  String response;

  public String connect(String str, String type)
  {
    try 
    {
        ConnectionRequest cr = new ConnectionRequest()
        {
            protected void readResponse(InputStream input)
            {
                try
                {
                    // just read from the response input stream
                    System.out.println("input length: "+input.available());
                    //byte[] b = new byte[input.available()];
                    //input.read(b, 0, input.available());
                    byte[] b ;
                    ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
                    int ch;
                    while ((ch = input.read()) != -1)
                        bStrm.write(ch);
                    b = bStrm.toByteArray();
                    System.err.println(b.length);                        
                    response = new String(b);                        
                    System.err.println(new String(b));
                }
                catch (IOException ex)
                {                        
                    System.err.println("IOException :"+ex.getMessage());
                }
            }
            protected void postResponse()
            {                    
            }
        };
        cr.setTimeout(3600);
        cr.setUrl(str.trim());     
        Progress progress = new Progress("Loading...", cr,true);
        progress.setAutoShow(true);
        progress.setDisposeOnCompletion(true);

        if (type.equals("POST"))
        {
            cr.setPost(true);
        } else
        {
            cr.setPost(false);
        }
        NetworkManager nm = NetworkManager.getInstance();
        nm.setTimeout(60000);
        nm.start();
        nm.addToQueueAndWait(cr);
        return response;
    }        
    catch (Exception ae)
    {
        System.out.println(ae.getMessage());
        return response;
    }
    finally
    {
        return response;
    }

  }

}



public class Progress extends Dialog implements ActionListener
{
    private ConnectionRequest request;
    private boolean disposeOnCompletion;
    private boolean autoShow;

    public Progress(String title, ConnectionRequest request) {
        this(title, request, false);
    }

    public Progress(String title, ConnectionRequest request, boolean showPercentage) {
        super(title);
        try {
            this.request = request;
            SliderBridge b = new SliderBridge(request);
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            addComponent(b);
            setDisposeWhenPointerOutOfBounds(false);
            setAutoDispose(false);
            NetworkManager.getInstance().addProgressListener(this);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void actionCommand(Command cmd) {
        if(Display.getInstance().isTouchScreenDevice() || getSoftButtonCount() < 2) {
            for(int iter = 0 ; iter < getComponentCount() ; iter++) {
                Component c = getComponentAt(iter);
                if(c instanceof Button) {
                    c.setEnabled(false);
                }
            }
        } else {
            removeAllCommands();
        }
        request.kill();
    }
    public boolean isDisposeOnCompletion() {
        return disposeOnCompletion;
    }
    public void setDisposeOnCompletion(boolean disposeOnCompletion) {
        this.disposeOnCompletion = disposeOnCompletion;
    }
    public void actionPerformed(ActionEvent evt) {
        NetworkEvent ev = (NetworkEvent)evt;
        if(ev.getConnectionRequest() == request) {
            if(disposeOnCompletion && ev.getProgressType() == NetworkEvent.PROGRESS_TYPE_COMPLETED) {
                dispose();
                return;
            }
            if(autoShow && Display.getInstance().getCurrent() != this) {
                show();
            }
        }
    }
    public boolean isAutoShow() {
        return autoShow;
    }

    public void setAutoShow(boolean autoShow) {
        this.autoShow = autoShow;
    }
}
4

1 回答 1

2

我认为您应该setProgress(value)在代码的某处使用该方法。而且您必须使用Thread.

看这里LWUIT 进度条

在该博客中,他们解释了如何使用线程将值设置为进度条。

于 2013-07-03T18:35:21.407 回答