POST UTF-8 encoded data to the server in J2ME

By: Ash Printer Friendly Format    

This sample code shows how to POST UTF-8 encoded data to the server in J2ME. The first requirement is to set the content-type for the connection. You should also use a OutputStreamWriter and pass "UTF-8" as the second parameter. Also note that the first parameter passed to this function 'data' should be encoded to UTF-8 bytes.

private void sendData(StringBuffer data,String serverUrl) throws IOException  
        HttpConnection connection = (HttpConnection),Connector.READ_WRITE);
        connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");
        connection.setRequestProperty("Accept-Encoding","gzip, deflate");
        InputStream inputStream = null;
        OutputStreamWriter out = null;
        byte[] readData = new byte[HTTP_INPUT_BUFFER_SIZE];        
        String response;       
            // --- write ---
            out = new OutputStreamWriter(connection.openOutputStream(),"UTF-8");            
            // --- read ---
            int responseCode = connection.getResponseCode();            
            if (responseCode != HttpConnection.HTTP_OK)
                throw new IOException("HTTP response code: " + responseCode);
            inputStream = connection.openInputStream();
            int actual =;
            if (actual==0)
                throw new IOException("No response from server");                
            response = new String(readData,0,actual,"UTF-8");
            if (!response.equalsIgnoreCase(SUCCESS_MESSAGE))
                throw new IOException(response);
            if (out!=null)
                } catch (IOException ex) {}
            if (inputStream!=null)
                } catch (IOException ex) {}

Ask a Question

Most Viewed Articles (in J2ME )

GUI components and menu based J2ME Applications.

Types of configurations in J2ME

Using HttpConnection in J2ME (Retrieve web content from a website to a phone)

Adding your own Application icon for your J2ME application (jar file)

What is J2ME?

Download a file over a network in J2ME midlet

How to load J2ME applications to the IDEN handsets

Y.S. Sun Green Building Research Center

'LINK.EXE' is not recognized as an internal or ext

Sample Java program shows how to Read a file over a network using J2ME midlet

Sample J2ME code that shows various functionality of RMS.

A sample J2ME midlet that shows an example of using a Timer and a TimerTask.

Your first J2ME program and a midlet lifecycle explained.

java.lang.SecurityException: MIDP lifecycle does not support system exit.

How to get the CLDC and MIDP version from a J2ME program

Latest Articles (in J2ME)

Comment on this tutorial

Ask a Question

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Great Sample, it helped me alot


View Tutorial          By: Mike at 2010-03-02 04:39:57

2. AntonioCrelp
View Tutorial          By: AntonioCrelp at 2017-06-03 11:45:06

3. AntonioCrelp
View Tutorial          By: AntonioCrelp at 2017-06-03 13:03:53

4. BarbaraRoG
View Tutorial          By: BarbaraRoG at 2017-08-16 12:43:48