×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Unhappy J2ME connection issue!! Application hangs! Help needed!

    Hi guys,

    I need some help. I have an issue with http connection in my J2me application. The problem is that the code for some reason hangs at getResponseCode(), and since there is no way to stop or abort a thread the app just hangs there. To sort this issue i created a connection watcher that after a few second of no activity calls the con.Close() on the method so that it can force it throw an IO interrupted exception and come out of it. But but but, it also hangs on the con.close() method as well. Now since the code hangs i have no way to make it come out of the thread loop. Obviously no point setting the loop boolean to false because the code is just stuck on that line and its not gona reach the bool.

    Has any one of you faced this sort of problem? Any ideas please?? I have been trying different things but no avail.

    ANy help will be appreciated.

    Regards

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: J2ME connection issue!! Application hangs! Help needed!

    For how long does it hang? Timeout is usually a couple of minutes.

    Once a connection starts, you cannot stop it. You have to wait for it to timeout.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Re: J2ME connection issue!! Application hangs! Help needed!

    Quote Originally Posted by grahamhughes View Post
    For how long does it hang? Timeout is usually a couple of minutes.

    Once a connection starts, you cannot stop it. You have to wait for it to timeout.
    Hi grahamhughes,

    thanks for your reply. The app hangs for a long time, i have waited for upto 15 mins. The wierd thing is that it does not connect to the server as i receive no request on the server.

    nomanm.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: J2ME connection issue!! Application hangs! Help needed!

    What device? Someone else posted recently about a connection hanging but never timing out.

  5. #5
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: J2ME connection issue!! Application hangs! Help needed!

    Hi,
    Is this giving some errors/exception/alerts?

    As you are saying that you have not received the HTTP request on the server.That means there is no question of response code,Give the answer of the following points -
    @ Which devise you are working with,
    @ Can you try this same code on other devices
    @ Please check the URL you are calling and make sure that this a valid URL,
    @ Can you put the HTTP code on the different thread,
    @ can you debug the code and make sure that up to where it is coming by putting the println statement,

    Please try all these point and put the output against every point here,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Re: J2ME connection issue!! Application hangs! Help needed!

    Hi.. i also have this problem. anyone can solve this?

    before this problem occured, it always got a response code (like 200, 401) without hang forever.
    i had 3 s40 phones. 2 phones already face this problem (Nokia 6288 & Nokia 6280) and another one dont have this problem (Nokia 3110)..

    help me please. Thank you.

  7. #7
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: J2ME connection issue!! Application hangs! Help needed!

    Hi,
    You must be getting only one response code..either the 200 or others....not more than 1 at a time..

    if you are getting the 200 then every thing is working like cool...then there something else is wrong..then check the code...
    can you please put the more information about your movements over the application ..?
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Re: J2ME connection issue!! Application hangs! Help needed!

    Hi.. i only get 1 response code at a time.
    case like this :
    i must connect to more than 1 url when app start.
    - first url will return code 200
    - second url will return 200
    - third url will return 401
    - forth url will return 200

    before this problem occured, it always can finish all.
    but while the problem occured, it can't reach the forth connection because the app will hang forever in getresponsecode on third connection .

    because of the getresponsecode hang forever, it cant close connection properly although i close httpconnection when close the app.
    so, after i close my app and open it again, when the app started connect to url, the handset will restart.

    has any idea?

  9. #9
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: J2ME connection issue!! Application hangs! Help needed!

    are u actually trying to connect to all 4 url's at the same time (4 threads) ? or are u working past them sequentally ?
    cause the number of active connections is different on most models, so one model will be capable of handling two simultanious conenction whilst the other might be able to handle 6 of them...
    also u are closing the con.close(), but are u also closing the inputstream and the outputstreams ?
    the right sequence is to close the streams first (input and output) and then the connection itself...
    also i read about closing a connection and starting a new one up right away (which u might be doing if u want to connect to 4 different url's sequentially), this was troublesome on some phones and some people found a workaround which consisted in waiting for some time before setting up the next (new) connection...

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Re: J2ME connection issue!! Application hangs! Help needed!

    hi, thanks for your reply.
    the connections worked past them sequentially.
    i had close all the connection before made a new connection.

    i used the same jar & jad and test on nokia 6280 many times and success. And suddenly this week, problem occurs which that phone can't handle 401 anymore, it'll get stuck at that "getResponseCode"

    if i need to wait for open next new connection, how long times should i wait?

    Thank you

  11. #11
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: J2ME connection issue!! Application hangs! Help needed!

    well I cant remember precisely, but afaik the problem was that under water it takes some time for the OS to really close the streams and connection and re-utilize the same objects to start up a new connection/streams...
    You coud try to start out by waiting 5 seconds and if that helps try to work your way down and see up to which point it will keep working, this might be device-dependable btw...

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Re: J2ME connection issue!! Application hangs! Help needed!

    Hi I too facing same issue on N72 App gets hand at httpCon.getResponseCode(), To cross check this I have written another MIDlet there its working fine, but the same logic failing to work in my main project.

    in both the cases I am using same URL, the same code is working fine on other devices like on E51, N95

    PFB the code... Can any one help me to solve this thanks in advance.


    public byte[] loadHttpFile(String url) throws IOException {

    HttpConnection httpCon = null;
    InputStream is = null;
    byte[] byteBuffer = null;

    textBox.setString(textBox.getString() + "\nConnecting to URL : " + url);

    try {
    httpCon = (HttpConnection) Connector.open(url);
    httpCon.setRequestMethod(HttpConnection.GET);
    int respCode = httpCon.getResponseCode();
    textBox.setString(textBox.getString() + "\nrespCode : " + respCode);

    /*
    * The requested resource has been assigned a new permanent URI or
    * resides temporarily under a different URI
    */
    if (respCode / 100 == 3) {

    int i = 1;
    String key = "";
    String value = "";

    while ((value = httpCon.getHeaderField(i)) != null) {
    key = httpCon.getHeaderFieldKey(i++);
    //textBox.setString(textBox.getString() + "\nkey : " + key);
    //textBox.setString(textBox.getString() + "\nvalue : " + value);
    System.out.println("key : " + key + "\t" + "value : " + value);
    }
    String newURL = httpCon.getHeaderField("location");
    //System.out.println("New URL : " + newURL);

    textBox.setString(textBox.getString() + "\nNew URL : " + newURL);
    try {
    if(httpCon != null) {
    httpCon.close();
    is.close();
    httpCon = null;
    }
    }
    catch (IOException ex) {
    ex.printStackTrace();
    }

    // Connect again at new url
    return loadHttpFile(newURL);
    }
    else if(respCode / 100 == 5) { // To handle 5XX response
    return null;
    }


    is = httpCon.openInputStream();
    int len = (int) httpCon.getLength();

    // Read data from InputStream
    byteBuffer = getByteStreamData(is, len, null);
    }
    finally {
    try {
    closeInputStream(is);
    is = null;
    if (httpCon != null) {
    httpCon.close();
    httpCon = null;
    }
    }
    catch(IOException e2) {
    e2.printStackTrace();
    }
    }
    return byteBuffer;
    }

    private byte[] getByteStreamData(InputStream is, int length,
    String encodedFormat)
    throws IOException {

    long b4 = System.currentTimeMillis();

    byte[] byteBuffer = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    if (length > 0) {
    int actual = 0;
    int bytesRead = 0 ;
    byte[] data = new byte[length];

    while ((bytesRead != length) && (actual != -1)) {
    actual = is.read(data, bytesRead, length - bytesRead);
    bytesRead += actual;
    }
    bos.write(data, 0, data.length);
    }
    else {
    int ch;
    while((ch = is.read()) != -1) {
    bos.write((char) ch);
    }
    }

    byteBuffer = bos.toByteArray();
    bos.close();
    bos = null;

    //System.out.println("Data arrived : \n" + new String(byteBuffer));
    return byteBuffer;
    }

    public void closeInputStream(InputStream is) throws IOException {
    if (is == null) {
    return;
    }
    is.close();
    }

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Re: J2ME connection issue!! Application hangs! Help needed!

    Hi,
    this sound much like what I have seen on a couple of Nokia 5800 XpressMusic, cf. my 'Use Access Point not shown when making HttpConnecton' I have included sourcecode for a simple MIDlet that simply tries to connect to an url and get the responseCode. I think the problem is that unless I first use the App Manager to select a fixed 'Access point' before starting the MIDlet the app just stops waiting for the user to select an 'Access point' without rendering the list of availabel access points on the screen. Have you selected a fixed Access Point on the E72? If not, can you please try doing that and let me know if you still have the problem?

    rgds,
    Dagfinn

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Re: J2ME connection issue!! Application hangs! Help needed!

    Hi thank you for your valuable info, for my both apps the device is prompting access points like airtelgprs, mobileOffice etc., but still I was facing same issue.

    I have solved this in some tricky manner, First I have used a high-level UI component then I made app to connect to some URL, after getting response I am displaying Canvas object.

    Solution : After several trail & errors I got to know we can solve this issue, if we send second request if the the very request faces hanging kind of issue.

    Note : Hanging has happened only for canvas object, but for high-level components it may appear blocked but by using some timer logic we can send 2nd request or by using Command events.

    Now I am facing another critical issue, my app(Canvas based) is getting hanged on N72 if I give any kind of interrupt(call, alarm, message, camera).

    - I am sure device is not calling pauseApp()!...
    - As the app is based on Canvas, it should call hideNotify/showNotify methods but after resuming app is simply hanging.

    Will some one help to solve this issue???

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Re: J2ME connection issue!! Application hangs! Help needed!

    Hi,
    I am facing the same issue where the request hangs up at getResonseCode() for quite a longer time ranging from 15 min to even sometimes 1 hour. I tried to close the connection from another thread but still the response code method dosent return with an exception.

    I tried putting the http close code the way mentioned in this thread but no success(stream should be closed first and then http connection). Eagerly looking for any solutions or workaround for this.

Similar Threads

  1. Replies: 5
    Last Post: 2010-02-11, 04:41
  2. HTTP connection in J2ME hangs N73
    By massenz in forum Mobile Java Networking & Messaging & Security
    Replies: 8
    Last Post: 2009-08-26, 21:56
  3. Replies: 1
    Last Post: 2008-09-25, 09:44
  4. Replies: 0
    Last Post: 2006-06-08, 09:04
  5. Replies: 1
    Last Post: 2003-10-17, 08:45

Posting Permissions

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