×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    193

    where to find HttpConnectionHelper.Callback

    hi i find a code from sun website which shows communication b/w j2me and servlets but it has some errors relating HttpConnectionHelper.Callback
    i found a thread on forum as well but it was not answered before.

    http://discussion.forum.nokia.com/fo...d.php?t=115764

    here is the link of original source code.
    http://java.sun.com/developer/J2METe...1218.html#tip2

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

    Re: where to find HttpConnectionHelper.Callback

    You can't compile it? HttpConnectionHelper.Callback is in the code in the article.

    Frankly, I think that code is an over-complicated approach, and I wouldn't use it.

    HTTP is quite simple, if you structure the code right.

    PHP Code:
    private static final String SERVLET_URL "http://www.myserver.com/whatever";

    /**
     * POST message to the servlet, return servlet's reply.
     */
    public String talkToServlet(String messagethrows IOException {
        
    String reply;

        
    HttpConnection con = (HttpConnectionConnector.open(SERVLET_URL);
        try {
            
    con.setRequestMethod(HttpConnection.POST);
            
    OutputStream out con.openOutputStream();
            try {
                
    // send the message
                
    out.write(message.getBytes("UTF-8"));

                
    // check the response
                
    int responseCode con.getResponseCode();
                if (
    responseCode != HttpConnection.HTTP_OK) {
                    
    // for example, if the URL is wrong, we might get 404 (not found)
                    
    throw new IOException("HTTP response code: " responseCode);
                }

                
    // all OK, read the reply
                
    InputStream in con.openInputStream();
                try {
                    
    // I've ignored the case where content-length is missing
                    
    int len = (int) con.getLength();
                    
    byte[] data = new byte[len];
                    
    int readSoFar 0;
                    while (
    readSoFar len) {
                        
    in.read(datareadSoFarlen readSoFar);
                    }
                    
    reply = new String(data"UTF-8");
                } 
    finally {
                    
    in.close();
                }
            } 
    finally {
                
    out.close();
            }
        } 
    finally {
            
    con.close();
        }

        return 
    reply;

    This structure makes sure everything gets closed after it's used, even if exceptions are thrown. This is important: if streams or connections are left open, you will eventually be unable to open any more connections.

    Cheers,
    Graham.

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

    Re: where to find HttpConnectionHelper.Callback

    Quote Originally Posted by grahamhughes View Post
    You can't compile it? HttpConnectionHelper.Callback is in the code in the article.

    Frankly, I think that code is an over-complicated approach, and I wouldn't use it.

    HTTP is quite simple, if you structure the code right.

    PHP Code:
    private static final String SERVLET_URL "http://www.myserver.com/whatever";

    /**
     * POST message to the servlet, return servlet's reply.
     */
    public String talkToServlet(String messagethrows IOException {
        
    String reply;

        
    HttpConnection con = (HttpConnectionConnector.open(SERVLET_URL);
        try {
            
    con.setRequestMethod(HttpConnection.POST);
            
    OutputStream out con.openOutputStream();
            try {
                
    // send the message
                
    out.write(message.getBytes("UTF-8"));

                
    // check the response
                
    int responseCode con.getResponseCode();
                if (
    responseCode != HttpConnection.HTTP_OK) {
                    
    // for example, if the URL is wrong, we might get 404 (not found)
                    
    throw new IOException("HTTP response code: " responseCode);
                }

                
    // all OK, read the reply
                
    InputStream in con.openInputStream();
                try {
                    
    // I've ignored the case where content-length is missing
                    
    int len = (int) con.getLength();
                    
    byte[] data = new byte[len];
                    
    int readSoFar 0;
                    while (
    readSoFar len) {
                        
    in.read(datareadSoFarlen readSoFar);
                    }
                    
    reply = new String(data"UTF-8");
                } 
    finally {
                    
    in.close();
                }
            } 
    finally {
                
    out.close();
            }
        } 
    finally {
            
    con.close();
        }

        return 
    reply;

    This structure makes sure everything gets closed after it's used, even if exceptions are thrown. This is important: if streams or connections are left open, you will eventually be unable to open any more connections.

    Cheers,
    Graham.
    Thanks Graham,
    I got solution .
    Now i can send email from j2me.
    Cheers mate!

Similar Threads

  1. Emulator: Can't find class file. Error
    By linlearn in forum Mobile Java Tools & SDKs
    Replies: 6
    Last Post: 2007-03-05, 12:20
  2. How to use Find box!
    By ctpthanh in forum Symbian User Interface
    Replies: 1
    Last Post: 2005-03-12, 10:37
  3. list box example
    By murmur20 in forum Symbian User Interface
    Replies: 1
    Last Post: 2004-11-09, 04:42
  4. list box with find box - Series 60
    By white_dragon in forum Symbian
    Replies: 3
    Last Post: 2004-08-16, 09:52
  5. Replies: 0
    Last Post: 2003-02-27, 20: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
  •  
×