请问如何控制一个线程里面的发送的网络请求?如果发送超过指定的时间而没有接受到响应,就断开这个连接及发送的请求, 重启开始连接并再次发送.如此循环往复.
这样的效果能否在一个线程里面完成?,该如何做?请高手说一下思路 . 谢谢.

解决方案 »

  1.   

    建立一个事务,该事务send request,必须要收到应答才能将该事务结束,如果该事务发出request后一段时间内没有收到应答(可用timer实现),则重发这个请求。
    为什么要在一个线程内完成??这样效率多低啊
    如果同时有1000个消息要发,难道要new一千个线程??
      

  2.   

    呵呵。这样吧。
    用布尔变量来控制。指定规定的时间没有响应就让线程挂起- wait,不是线程本身的wait,是对象的wait,如果需要重用线程就释放对象的线程列表notifyAll或者notify,最好用notifyAll。
    代码框架:
    //添加Runnable接口
    isWait : boolean ; //是否暂停
    ................
    //重写RUN方法
    void run(){
         if(isWait){ //如果isWait被设置为true就执行循环体,另外就wait      //do more work..     }else
            wait();
    }//释放
    public void XXXXX(){
        对象.NotifyAll();
        线程.start();
    }
      

  3.   

    如何判断这个时间啊? 发送网络请求等待及返回的时间.
    定时检测?好像很难实现啊. 
    我这个程序还是要写在j2me里面的. 发送请求只能写在另外一个线程.
      

  4.   

    public class HttpPoster implements Runnable { private synchronized void doSend(String request, String host,HttpPosterListener listener) 
    {
        String responseStr = null;    
        HttpConnection hpc = null;
        DataInputStream dis = null;
        boolean newline = false;
        String content = "";
        try {
          // ===========================================================
          // 建立连接
          // ===========================================================
          //hpc = (HttpConnection) Connector.open(request);
          hpc = (HttpConnection) Connector.open(request);  //手机代理      
          hpc.setRequestMethod(HttpConnection.GET);
    //      hpc.setRequestProperty("Connection", "close");
          //hpc.setRequestProperty("Connection", "Keep-Alive");
          hpc.setRequestProperty("accept","text/html, application/xhtml+xml, application/vnd.wap.xhtml+xml, text/vnd.wap.wml, application/vnd.wap.wmlc, application/vnd.wap.wbxml, application/vnd.wap.wmlscriptc, application/vnd.wap.multipart.mixed, multipart/mixed, text/x-vcard, text/x-vcalendar, audio/*, application/*, */*, image/gif, image/vnd.wap.wbmp");
          hpc.setRequestProperty("cache-control","no-cache");
          hpc.setRequestProperty("user-agent","SonyEricssonP908/R101 Profile/MIDP-2.0 Configuration/CLDC-1.0");
          hpc.setRequestProperty("accept-language","zh, en");
          hpc.setRequestProperty("accept-charset","utf-8, utf-16, iso-8859-1, *");
          hpc.setRequestProperty("x-wap-profile","http://www.sonyericsson.com/UAProf/P900R101.xml");                  
          hpc.setRequestProperty("X-Online-Host", host);  //在手机测试需要
          //dis = new DataInputStream(hpc.openInputStream());
          dis = hpc.openDataInputStream();
          int character;
          // ===========================================================
          // 读取返回的HTTP内容
          // ===========================================================
          while ((character = dis.read()) != -1) {
            if ((char) character == '\\') {
              newline = true;
              continue;
            } else {
              if ((char) character == 'n' && newline) {
                content += "\n";
                newline = false;
              } else if (newline) {
                content += "\\" + (char) character;
                newline = false;
              } else {
                content += (char) character;
                newline = false;
              }
            }
          }
        } catch (IOException e) {
          System.out.print("ERROR:" + e);
        } finally {
          if (hpc != null) {
            try {
              hpc.close();
            } catch (Exception e) {
            }
            hpc = null;
          }
          if (dis != null) {
            try {
              dis.close();
            } catch (Exception e) {        }      }
        }
    .........如何检测网络连接及发送耗费的时间?? 如果超过指定的一个时间,比如1分钟,就重新连接.
      

  5.   

    如何判断这个时间啊? 发送网络请求等待及返回的时间.
    定时检测?好像很难实现啊. 
    我这个程序还是要写在j2me里面的. 发送请求只能写在另外一个线程.
    ------------------------------------
    获取时间:
    发送消息时获取当前时间:
    long befor = System.currentTimeMills()
    不管是成功或者失败都回返回的,在返回方法中获取当前时间:
    long after = System.currentTimeMills();long delay = befor - after ;//超时 如果超时就让线程进行等待wait或者不活动join