2011-06-28 50 views
1

我需要在j2me设备上同时打开两个http连接。所以我在两个线程中打开了两个HttpConnection。但有时一个连接收到另一个连接的数据。我怎么解决这个问题?
我在诺基亚N70上测试应用程序。J2me多线程http数据泄露来自另一个线程

代码很大。我尝试写一些简单的伪代码。

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://....") ; 
+0

Ç ö请

+1

请告诉我们你是如何做到这一点。 –

回答

2

我认为你需要使用​​关键字在getHttp()方法

相关问题