我想使用进度窗格从这里返回响应,以便显示加载对话框。代码运行良好,但有时我无法从这里获得响应。
代码段是:
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;
}
}
我认为您应该在代码的某个地方使用setProgress(value)
方法。并且必须使用Thread
。
看看这里LWUIT进度条
在博客中,他们解释了如何使用线程设置进度条的值。