×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    33

    Cancelling an http connection

    I'm trying to give the user the ability to cancel a long running http connection. The "standard" advice is to close the http Connection. This works on the emulator by causing an IOException. But on the the device (7650, 6100) it seems to do nothing.

    My code is ...
    try {
    deBug(6,"kill commsHc"); // http connection
    commsThread.pollHc.close();
    }catch (Exception ex) {
    deBug(3,"npe closing pollHc");
    }
    try {
    deBug(6,"kill commsOut"); // output data stream
    commsThread.pollOut.close();
    }catch (Exception ex) {
    deBug(3,"npe closing pollOut");
    }
    try {
    deBug(6,"kill commsIn"); //input datastream
    commsThread.pollIn.close();
    }catch (Exception ex) {
    deBug(3,"npe closing pollIn");
    }

    Any ideas ?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    41
    I've come across the same problem. One solution is to kill off the object entirely - assuming it's running in a separate thread. The object will eventually get garbage collected. This will only work if you are needing to do this infrequently.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    I'm also doing a cancel option that doesn't seem to work on Nokia models. (It works on Sony Ericsson, though... sigh!!)

    For me, whenever I call the HttpConnection.close() method, my connection thread seems to hang at HttpConnection.openInputStream() ... typically, it's at this point where I try to cancel --- when the server I'm connected to takes a long time to send back data.

    Has anyone figured out how to do this properly?

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    For Threads


    Obviously if you close InputStream , HttpConnection & OutputStream then you can get rid of long running HttpConnection. But if are running HttpConnection in saperate thread then there is a problem.

    Thread.stop is not available. So you can just yield the thread & nullified it. Therotically it should solve the problem but in this real world it can't.

    The very successful method is use flag. In run method just check the flag & exit from run method.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    FYI, I'm basing my connection class on the following article (see DeluxeWorker):

    http://wireless.java.sun.com/midp/articles/threading/

    Of course, I've changed it so extensively that it's so difficult to go back and try to fix it. (wry) I'm kicking myself for not testing it on more models --but anyway!

    I suppose that a flag could work, although I see it could lead to a very bloated connection class where a check for the flag would occur after each statement in the connection method.

    But then again, it seems to me that I would normally want to cancel when it has hit either Connection.open() or HttpConnection.openInputStream() and has not/could not finish/ed... So, even if I set a flag, I'd still end up waiting (forever?), because the flag statement would never be reached.

    I've seen this "hanging" occur in the following situations:

    - connection was made to a server, but the server did not respond nor did it release the connection. (Yes, the fault's at the server-end, and I've fixed that, but shouldn't the client be able to terminate the connection?)

    - GPRS connection dies halfway.

    - on certain phones (3300), if the url is not properly url encoded. This is fixable, but it's a PAIN! as (again) no exception was thrown. It just ..hangs there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×