我需要在j2me设备上同时打开两个http连接。所以我在两个线程中打开了两个httpconnection。但有时一个连接接收到另一个连接的数据。我怎样才能解决这个问题?
我在诺基亚N70上测试应用程序。
代码很大。我试着写一些简单的伪代码。
http.java:
public class Http
{
public Http()
{
}
public void start(String url) {
new Thread() {
public void run() {
getHttp(url);
}
}.start();
}
private void getHttp(String url) {
InputStream is = null;
HttpConnection http=null ;
try {
http= (HttpConnection) Connector.open(url);
httpCode = http.getResponseCode();
is = http.openInputStream();
int ic;
byte[] tmp = new byte[1024];
while (!cancel && (ic = is.read(tmp, 0, 1024)) != -1) {
line.append((char) ic);
bao.write(tmp, 0, ic);
}
//httpnotify.receive(bao.toByteArray) ;
} catch (Exception e) {
}
}
}
客户1:
Http http=new Http() ;
http.setNotify(self) ;
http.start("http://....") ;
客户2:
Http http2=new Http() ;
http2.setNotify(self) ;
http2.start("http://....") ;
最佳答案
我认为您需要在synchronized
方法上使用getHttp()
关键字