我想在j2me中从这里返回响应

时间:2013-07-03 10:08:52

标签: java lwuit j2mepolish

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

代码段是:

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;
    }
}

1 个答案:

答案 0 :(得分:2)

我认为你应该在代码的某个地方使用setProgress(value)方法。你必须使用Thread

看看LWUIT Progress bar

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