gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:20:22 24 4
gpt4 key购买 nike

我想使用进度 Pane 从此处返回响应,以便我可以显示加载对话框。代码运行良好,但有时我无法从此处获得响应。

代码片段是:

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 Progress bar

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

关于java - 我想在 j2me 中从这里返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com