×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    HTTP CONNECTION PROBLEM

    Hello,

    I am having problem downloading a text file vile htto connection.
    I am the source code below to downloand the file and it is working well in any emulator and on the motorola razor phone.

    But when i tried testing them on a nokia 3220 phone(ser 40), is throwingthis exception:

    java.io.ioException: error in http operation

    it is throwing exception on this line

    is = c.openInputStream();

    when i tried adding a get response code

    int rc = c.getResponseCode();

    it throws the exception in this line

    Below is my source code:

    /*********************source code***************** */
    public String getDataFromUrl(String url) //throws IOException
    {
    int rc=0;
    try{
    StringBuffer b = new StringBuffer();
    InputStream is = null;
    HttpConnection c = null;
    c = (HttpConnection)Connector.open(url);
    // Getting the response code will open the connection,
    // send the request, and read the HTTP response headers.
    // The headers are stored until requested.
    is = c.openInputStream();
    // Get the ContentType
    String type = c.getType();
    // Get the length and process the data
    int len = (int)c.getLength();
    if (len > 0) {
    byte[] raw = new byte[len];
    int length = is.read(raw);
    b = new StringBuffer(new String(raw,0,length));
    }else {
    int ch;
    while ((ch = is.read()) != -1)
    {
    len = is.available() ;
    b.append((char)ch);
    }
    }
    is.close();
    return b.toString();
    }catch(Exception e){
    return null;
    }
    }

  2. #2
    Nokia Developer Expert
    Join Date
    Jun 2005
    Posts
    923

    Re: HTTP CONNECTION PROBLEM

    Hi,

    Couple of observations:

    1) Are you using an Internet Access Point, instead of a WAP one?
    2) Your code has some inconsistencies:

    if (len > 0) {
    byte[] raw = new byte[len];
    int length = is.read(raw); // THIS IS NOT GUARANTEED TO WORK, THE READ METHOD MAY OR MAY NOT READ THE ENTIRE FILE. THE ONLY GUARANTEED METHOD IS DATAINPUTSTREAM.READFULLY
    b = new StringBuffer(new String(raw,0,length));

    len = is.available() ;// you keep calling this every time in the "else" clause. No need.

    Lastly, you call close() before returning the value. You should do it withing the "finally" clause.

    Daniel

  3. #3
    Super Contributor
    Join Date
    Mar 2006
    Location
    Phoenix, AZ. USA
    Posts
    556

    Re: HTTP CONNECTION PROBLEM

    dont you also have to sometimes get response code, before opening input stream? so as to flush the headers and create connection?
    Jason Glass
    http://IChiBanComputers.Com

  4. #4
    Nokia Developer Expert
    Join Date
    Jun 2005
    Posts
    923

    Re: HTTP CONNECTION PROBLEM

    Yes, it is also a good practice to get the response codes, so you can flush the headers and catch errors such as 500 (very frequent), 404, 403, etc.

    Daniel

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    3

    Re: HTTP CONNECTION PROBLEM

    I'm getting an error code 403. How do I catch that error and flush the headers?

  6. #6
    Super Contributor
    Join Date
    Mar 2006
    Location
    Phoenix, AZ. USA
    Posts
    556

    Re: HTTP CONNECTION PROBLEM

    figure it out phoenix?
    Jason Glass
    http://IChiBanComputers.Com

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    55

    Re: HTTP CONNECTION PROBLEM

    At every Http connection, please check the response by adding this at every http connection.

    int respcode = connection.getResponseCode();
    if (respcode != HttpConnection.HTTP_OK)
    {
    mStat = new Alert("Error", "Error code:"+respcode, null, AlertType.INFO);
    mStat.setTimeout(Alert.FOREVER);
    disp.setCurrent(alStatus);
    }
    else
    {
    }

    Regards,
    Jac

  8. #8
    Super Contributor
    Join Date
    Mar 2006
    Location
    Phoenix, AZ. USA
    Posts
    556

    Re: HTTP CONNECTION PROBLEM

    jaceline

    also when porting code amongst manufacturers, I have found now that does not always work, if you connect through a wrong wap gatewy, sometimes you get 200, but then, when you try to use the connection, you get an exception

    seems to happen more often if the WAP gateway is involved, as compared to explicitly in code not specifying a wap gateway, and in the case of https

    Just FYI, as it gave me a headache
    Jason Glass
    http://IChiBanComputers.Com

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    55

    Re: HTTP CONNECTION PROBLEM

    Just make sure you insert the try-catch to catch the exception error. It will not be our fault for having the exception error.Just make sure everything is properly controlled and not stuck without knowing the exception

Similar Threads

  1. HTTP Connection problem with real series 40 device
    By pagadi in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2004-06-17, 09:28
  2. HTTP Connection problem on Nokia 3650
    By niko86 in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2004-04-16, 15:53
  3. problem using http: connection is paused or loss without reason
    By pablito816@hotmail.com in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2003-11-11, 23:06
  4. NOKIA vs. SIEMENS HTTP connection problem?
    By imarenic in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2003-11-03, 09:03
  5. HTTP connection problem on 7650
    By gpeschiera in forum Mobile Java General
    Replies: 1
    Last Post: 2002-10-30, 03:08

Posting Permissions

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