×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    254

    HttpConnection in S40 devices

    in s40 while i am reading i read it but when i try to write i am unable to write to a web server
    plz tell me where i am going wrong


    try {
    String data ="InputStream ins = hconn.openInputStream()";
    byte dataarray[]=data.getBytes();
    HttpConnection hconn = (HttpConnection) Connector.open("http://"http://wwwr.com/demo/UploadFile?userName=joel&size="+dataarray.length);
    hconn.setRequestMethod("GET");

    responsecode = hconn.getResponseCode();
    responsemsg =hconn.getResponseMessage();
    InputStream ins = hconn.openDataInputStream();
    int ch=-1;

    while((ch=ins.read())!=-1)
    {
    System.out.print((char)ch);
    userdata+=(char)ch;
    }

    ins.close();
    OutputStream os = hconn.openDataOutputStream();
    for(int i=0;i<dataarray.length;i++)
    os.write(dataarray[i]);
    os.flush();
    os.close();
    hconn.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    regards
    Arun Gupta

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

    Re: HttpConnection in S40 devices

    HTTP doesn't work this way. You send a request, you get a response, end. So, you must send data first, then read. You cannot do it the other way around. If you want to send more data, you must send it in another request.

    Cheers,
    Graham.

  3. #3
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    254

    Re: HttpConnection in S40 devices

    Quote Originally Posted by grahamhughes View Post
    HTTP doesn't work this way. You send a request, you get a response, end. So, you must send data first, then read. You cannot do it the other way around. If you want to send more data, you must send it in another request.

    Cheers,
    Graham.
    when i send the request i receive the request in the doget method where i am opening the InputStream for reading in the server.
    so my work is like i will infom the serverv about the user (username)and size of the data by length

    the reading code i added just to check whether it is working or not
    and it is working..
    the final code i am using is like this

    try {
    String data ="InputStream ins = hconn.openInputStream()";
    byte dataarray[]=data.getBytes();
    HttpConnection hconn = (HttpConnection) Connector.open("http://"http://wwwr.com/demo/UploadFile?userName=joel&size="+dataarray.length);
    hconn.setRequestMethod("GET");

    responsecode = hconn.getResponseCode();
    responsemsg =hconn.getResponseMessage();

    OutputStream os = hconn.openDataOutputStream();
    for(int i=0;i<dataarray.length;i++)
    os.write(dataarray[i]);
    os.flush();
    os.close();
    hconn.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }

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

    Re: HttpConnection in S40 devices

    You must write first, then read. "Reading" includes getting the response code. You must send your complete request before attempting to read any of the response.

    Also: you can't write content if you are using "GET". You need to "POST".

    Also: you must ensure that connections are closed in all situations. Otherwise your application will eventually stop working.

    PHP Code:
    String data ="InputStream ins = hconn.openInputStream()"
    byte dataarray[]=data.getBytes();
    HttpConnection hconn = (HttpConnectionConnector.open("http://wwwr.com/demo/UploadFile?userName=joel&size="+dataarray.length);
    try {
        
    hconn.setRequestMethod(HttpConnection.POST);

        
    OutputStream os hconn.openDataOutputStream();
        try {
            
    os.write(dataarray);
        } 
    finally {
            
    os.close();
        }

        
    responsecode hconn.getResponseCode();
        
    responsemsg hconn.getResponseMessage();

    finally {
        
    hconn.close();

    Beware of using getBytes(): it is not always unicode-safe.

    Cheers,
    Graham.

  5. #5
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    254

    Re: HttpConnection in S40 devices

    Quote Originally Posted by grahamhughes View Post
    You must write first, then read. "Reading" includes getting the response code. You must send your complete request before attempting to read any of the response.

    Also: you can't write content if you are using "GET". You need to "POST".

    Also: you must ensure that connections are closed in all situations. Otherwise your application will eventually stop working.

    PHP Code:
    String data ="InputStream ins = hconn.openInputStream()"
    byte dataarray[]=data.getBytes();
    HttpConnection hconn = (HttpConnectionConnector.open("http://wwwr.com/demo/UploadFile?userName=joel&size="+dataarray.length);
    try {
        
    hconn.setRequestMethod(HttpConnection.POST);

        
    OutputStream os hconn.openDataOutputStream();
        try {
            
    os.write(dataarray);
        } 
    finally {
            
    os.close();
        }

        
    responsecode hconn.getResponseCode();
        
    responsemsg hconn.getResponseMessage();

    finally {
        
    hconn.close();

    Beware of using getBytes(): it is not always unicode-safe.

    Cheers,
    Graham.
    Thanks a lot for your suggestion i got what mistake i was doing

    regards
    Arun Gupta

  6. #6
    Super Contributor
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Thumbs up Re: HttpConnection in S40 devices

    Hi,
    Please set the connection POST while doing reading and writing both. can use SocketConnection.
    thanks,
    jitu_goldie..

    KEEP TRYING..

  7. #7
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    254

    Re: HttpConnection in S40 devices

    thanks
    JituGoldie,
    i will try that but my work is done through POST method...

    regards
    Arun Gupta

Similar Threads

  1. Memory Usage more with S40 devices
    By muralidharvvk in forum Mobile Java General
    Replies: 6
    Last Post: 2008-06-03, 08:23
  2. Problem with HTTPConnection in nokia6280 (S40 3rd edittion)
    By yair.zaslavsky in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2006-05-17, 06:33
  3. Emulatros of S40 devices
    By venkat512 in forum Mobile Java Tools & SDKs
    Replies: 1
    Last Post: 2006-03-15, 17:00
  4. Which S40 devices support JSR-75?
    By rolloderwikinger in forum Mobile Java General
    Replies: 2
    Last Post: 2006-01-28, 19:20
  5. HttpConnection error only in devices
    By D_Guidi in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2004-09-26, 17:28

Posting Permissions

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