Results 1 to 14 of 14
  1. #1
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default why this HTTPPost class is not working on Samsung phones and emulator?

    I'am using this class to send post data in j2me. But it's working only on Nokia phones. Not on netbeans emulators nor on Samsung phones it's not sending post data. can anyone help me to solve this issue?

    Java Code:
    /*    */ import java.io.ByteArrayOutputStream;
    /*    */ import java.io.IOException;
    /*    */ import java.io.InputStream;
    /*    */ import java.io.OutputStream;
    /*    */ import javax.microedition.io.Connector;
    /*    */ import javax.microedition.io.HttpConnection;
    /*    */ 
    /*    */ public class HttpPost
    /*    */ {
    /*    */   public final String response;
    /*    */ 
    /*    */   public HttpPost(String url, String msg)
    /*    */     throws IOException
    /*    */   {
    /* 15 */     byte[] data = null;
    /* 16 */     InputStream istrm = null;
    /* 17 */     HttpConnection http = (HttpConnection)Connector.open(url);
    /* 18 */     http.setRequestMethod("POST");
    /* 19 */     http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    /* 20 */     http.setRequestProperty("User-Agent", "PAL.AZ IM/1.0");
    /* 21 */     http.setRequestProperty("Content-length", "" + msg.getBytes().length);
    /* 22 */     OutputStream out = http.openOutputStream();
    /* 23 */     out.write(msg.getBytes());
    /* 24 */     out.flush();
    /* 25 */     if (http.getResponseCode() == 200) {
    /* 26 */       int len = (int)http.getLength();
    /* 27 */       istrm = http.openInputStream();
    /* 28 */       if (istrm == null)
    /* 29 */         throw new IOException("Cannot open HTTP InputStream, aborting");
    /*    */       int bytesRead;
    /* 31 */       if (len != -1) {
    /* 32 */         data = new byte[len];
    /* 33 */         bytesRead = istrm.read(data);
    /*    */       } else {
    /* 35 */         ByteArrayOutputStream bo = new ByteArrayOutputStream();
    /*    */ 
    /* 37 */         int count = 0;
    /*    */         int ch;
    /* 42 */         while ((ch = istrm.read()) != -1) {
    /* 43 */           bo.write(ch);
    /* 44 */           count++;
    /*    */         }
    /* 46 */         data = bo.toByteArray();
    /* 47 */         bo.close();
    /*    */       }
    /* 49 */       this.response = new String(data);
    /*    */     } else {
    /* 51 */       this.response = null;
    /*    */     }
    /*    */ 
    /* 57 */     http.close();
    /*    */   }
    /*    */ }

  2. #2
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    why nobody want to help?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    Why does the posted code have the /* */ in the first columns?

  4. #4
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    i lost my source codes. i have decompiled jar file. i get this from it. but issue isn't in those comments, i have this class on other computer, decompiling not changed the code.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    I think your first step is to post a clean code sample. It is hard to say why something doesn't work when all you have to look at is a mess of comments and numbers. Could you clean up the code and describe exactly what doesn't work?

  6. #6
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    clean code.
    Java Code:
    package main;
    /**
     *
     * @author Nadir Novruzov
     */
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Vector;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    
    public class HttpPost {
        public final String response;
            nodes.addElement(original);
    
            String[] result = new String[nodes.size()];
            if (nodes.size() > 0) {
                for (int loop = 0; loop < nodes.size(); loop++)
                {
                    result[loop] = ((String)nodes.elementAt(loop));
                }
            }
            return result;
        }
        public HttpPost(String url, String msg) throws IOException{
            byte[] data = null;
            InputStream istrm = null;
            
            HttpConnection con = (HttpConnection)Connector.open(url);
            con.setRequestMethod(HttpConnection.POST);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestProperty("User-Agent", "PAL.AZ Chat/1.0");
            con.setRequestProperty("Content-length", ""+msg.getBytes().length);
            
            OutputStream out = con.openOutputStream();
            out.write(msg.getBytes());
            //out.close();
            if (con.getResponseCode() == HttpConnection.HTTP_OK) {
                int len = (int)con.getLength();
                istrm = con.openInputStream();
                if (istrm == null) {
                    throw new IOException("Cannot open HTTP InputStream, aborting");
                }
                if (len != -1) {
                    data = new byte[len];
                    int bytesRead = istrm.read(data);
                } else {
                    ByteArrayOutputStream bo = new ByteArrayOutputStream();
                    int ch;
                    int count = 0;
    
                    // This is obviously not particularly efficient
                    // You may want to use a byte[] buffer to read bytes in chunks
                    //
                    while ((ch = istrm.read()) != -1) {
                        bo.write(ch);
                        count++;
                    }
                    data = bo.toByteArray();
                    bo.close();
                }
                response = new String(data);
            } else {
                response = null;
            }
            // This is critical, unless you close the HTTP connection, the application
            // will either be consuming needlessly resources or, even worse, sending
            // 'keep-alive' data, causing your user to foot unwanted bills!
            //
            out.close();
            con.close();
        }
    }
    post data not sent on netbeans emulator and samsung phones.

    for example if you call

    Java Code:
    HTTPPost test = new HTTPPost("http://example.com/example.php","username=test&password=test");
    username and password will not be sent.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    Not sure about tis, but I dont see you flushing the output stream before you try to read a response. That means the message would be stuck in the buffer and never sent. Where you have the line commented out "out.close()" insert out.flush(). What happens?

  8. #8
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    now code is like this. nothing changed, that's not working either
    Java Code:
    package main;
    /**
     *
     * @author Nadir Novruzov
     */
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    
    public class HttpPost {
        public final String response;
        
        public HttpPost(String url, String msg) throws IOException{
            byte[] data = null;
            InputStream istrm = null;
            
            HttpConnection con = (HttpConnection)Connector.open(url);
            con.setRequestMethod(HttpConnection.POST);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestProperty("User-Agent", "PAL.AZ Chat/1.0");
            con.setRequestProperty("Content-length", ""+msg.getBytes().length);
            
            OutputStream out = con.openOutputStream();
            out.write(msg.getBytes());
            out.flush();
            out.close();
            if (con.getResponseCode() == HttpConnection.HTTP_OK) {
                int len = (int)con.getLength();
                istrm = con.openInputStream();
                if (istrm == null) {
                    throw new IOException("Cannot open HTTP InputStream, aborting");
                }
                if (len != -1) {
                    data = new byte[len];
                    int bytesRead = istrm.read(data);
                } else {
                    ByteArrayOutputStream bo = new ByteArrayOutputStream();
                    int ch;
                    int count = 0;
    
                    // This is obviously not particularly efficient
                    // You may want to use a byte[] buffer to read bytes in chunks
                    //
                    while ((ch = istrm.read()) != -1) {
                        bo.write(ch);
                        count++;
                    }
                    data = bo.toByteArray();
                    bo.close();
                }
                response = new String(data);
            } else {
                response = null;
            }
            // This is critical, unless you close the HTTP connection, the application
            // will either be consuming needlessly resources or, even worse, sending
            // 'keep-alive' data, causing your user to foot unwanted bills!
            //
            con.close();
        }
    }

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    What happens if you run something like wireshark? Do you see it creating a packet and sending it out?

  10. #10
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    packet sent, but i cannot see what is sent.
    Last edited by NModern; 01-03-2012 at 09:22 PM.

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    packet sent, but i cannot see what is sent.
    If you cannot see what was sent, how do you know it was sent? Wireshark lets you inspect the packet contents, you would def. be able to see something simple like an http post.

  12. #12
    sandypop is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    Hey NModern, u find any solution on this.
    m getting same problem on Samsung C6112.

  13. #13
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    No. I havn't found any solution.

  14. #14
    NModern is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Re: why this HTTPPost class is not working on Samsung phones and emulator?

    I found a new way of sending HTTPPost requests. this is method not a class

    Java Code:
    public String sendHTTPPost(String url, String params) throws IOException {
    
        HttpConnection httpConn = null;
        InputStream is = null;
        OutputStream os = null;
        String response = "";
        try {
          // Open an HTTP Connection object
          httpConn = (HttpConnection)Connector.open(url);
          // Setup HTTP Request to POST
          httpConn.setRequestMethod(HttpConnection.POST);
    
          httpConn.setRequestProperty("User-Agent", "PAL.AZ IM");
          httpConn.setRequestProperty("Accept_Language","en-US");
          //Content-Type is must to pass parameters in POST Request
          httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    
          // This function retrieves the information of this connection
          //getConnectionInformation(httpConn);
    
    
          os = httpConn.openOutputStream();
    
          os.write(params.getBytes());
    
          /**Caution: os.flush() is controversial. It may create unexpected behavior
                on certain mobile devices. Try it out for your mobile device **/
    
          //os.flush();
    
          // Read Response from the Server
    
          StringBuffer sb = new StringBuffer();
          is = httpConn.openDataInputStream();
          int chr;
          while ((chr = is.read()) != -1)
            sb.append((char) chr);
          response =  sb.toString();
        } finally {
            if(is!= null)
               is.close();
              if(os != null)
                os.close();
          if(httpConn != null)
                httpConn.close();
        }
            return response;
        }

Similar Threads

  1. Samsung Emulator
    By pabloma2002 in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 03-11-2011, 03:35 PM
  2. sending message to phones
    By sivasankar in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-16-2011, 08:56 AM
  3. Sending sms from pc to mobile phones
    By leoanto in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 12-02-2010, 03:07 PM
  4. Replies: 1
    Last Post: 08-07-2009, 09:30 AM

Tags for this Thread

Posting Permissions

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