shashank kapoor
shashank kapoor

Reputation: 45

Unable to do HTTP POST from J2ME (Nokia S40 6thEdition)

Im unable to do HTTP POST using J2ME below is the code that im using which is throwing an exception when I try and write to the OutputStrem.

In the code Below SYSO prints "here5". Please need some guidance. Basically I've put the http connection part in a separate threads run method, to keep it aloof from the UI thread.

public void run(){
    HttpConnection http = null;
    OutputStream out = null;
    String msg = "lat=10&long=20&mac=923873";
    try{
    String url = "http://xxxx.php"; 
    byte[] data = null;
    InputStream istrm = null;

    http = (HttpConnection)Connector.open(url);
    }
    catch(Exception e)
    {
        System.out.println("here1");
    }
    try
    {
        http.setRequestMethod(HttpConnection.POST);
    }
    catch(Exception e)
    {
        System.out.println("here2");
    }

    try{
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.setRequestProperty("User-Agent", "HttpMidlet/0.2");
        http.setRequestProperty("Custom-Property", "MyCustomProperty/1.0; AnotherProperty/debug_0.1");

        http.setRequestProperty("Content-Length", ""+msg.getBytes().length);
    }
    catch(Exception e)
    {
        System.out.println("here3");
    }

    try{

        out = http.openOutputStream();
    }
    catch(Exception e)
    {
        System.out.println("here4");
    }
    try{
    out.write(msg.getBytes());
    out.flush();
    }
    catch(Exception e)
    {
        System.out.println("here5");
    }

}

Upvotes: 1

Views: 546

Answers (3)

Steven Mark Ford
Steven Mark Ford

Reputation: 3432

I resolved this by re-installing the emulator.

Upvotes: 0

Douglas Frari
Douglas Frari

Reputation: 4377

/**
     * Send the data to the URL of Server Site using the POST connection.
     * 
     * @return the response of server.
     * @throws Exception
     */
    public byte[] send() throws Exception {
        HttpConnection hc = null;
        InputStream is = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte[] res = null;

        try {
            hc = (HttpConnection) Connector.open(url);

            hc.setRequestProperty("Content-Type",
                    "multipart/form-data; boundary=" + getBoundaryString());

            hc.setRequestMethod(HttpConnection.POST);



            OutputStream dout = hc.openOutputStream();

            dout.write(postBytes);
            if (dout!=null) {
                dout.close();
                dout = null;
            }

            int ch;
            is = hc.openInputStream();

            while ((ch = is.read()) != -1) {
                bos.write(ch);
            }
            res = bos.toByteArray();
        } catch (Exception e) {
            // if an error occurred connecting to the server.
            throw new Exception(e.getMessage());

        } finally {
            try {
                if (bos != null)
                    bos.close();

                if (is != null)
                    is.close();

                if (hc != null)
                    hc.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return res;
    }

Upvotes: 1

Telmo Pimentel Mota
Telmo Pimentel Mota

Reputation: 4043

According to this Nokia Sample you do not need to call setRequestProperty methods.

Another interesting point is the usage of Connector.open(urlstring, Connector.READ_WRITE).

Upvotes: 0

Related Questions