4

Greetings,

I am trying to setup a server connection from my BlackBerry Application . I was able to get a response code on the status of the server. Now i have a few values which i have to POST to the server

Its like a registration page values(username, password, age ) have to be sent to the server .

        ConnectionFactory connFact = new ConnectionFactory();
        ConnectionDescriptor connDesc;
        connDesc = connFact.getConnection(url);
        if (connDesc != null)
        {
            HttpConnection httpConn;
            httpConn = (HttpConnection)connDesc.getConnection();
            try
            {
                final int iResponseCode = httpConn.getResponseCode();
                UiApplication.getUiApplication().invokeLater(new Runnable()
                {
                    public void run()
                    {
                        Dialog.alert("Response code: " + Integer.toString(iResponseCode));
                    }
                });
            }
            catch (IOException e)
            {
                System.err.println("Caught IOException: " + e.getMessage());
            }
        }

Thats the code i used to get the response code. I would appreciate it if someone could help me how i can make a POST request to the server.. the server url for status was company.com/app/version/stats

when it for register it would be company.com/app/register

Thank you

KevinDTimm
  • 13,876
  • 3
  • 39
  • 59
alanvabraham
  • 699
  • 1
  • 13
  • 25

2 Answers2

6

What type of a POST do you use? If you are just passing key-value pairs, then it should be a POST of a "application/x-www-form-urlencoded" content-type.

So, what lacks youe code is:

1). Set a proper content-type on your connection:

httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

2). Prepare the content to be sent to the server via the POST:

URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("username", username);
encPostData.append("password", password);
encPostData.append("age", age);
byte[] postData = encPostData.toString().getBytes("UTF-8");

3). Set content-length for the connection (this step may be optional - try without this first, probably the BB OS is smart enough to set this automatically):

httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));

4). Open an OutputStream and write the content to it (the code is simplified):

OutputStream os = httpConn.openOutputStream();
os.write(postData);
os.flush();
Vit Khudenko
  • 27,639
  • 10
  • 56
  • 86
  • Hi, It is giving null pointer exception. Something related to OutputStream and postData, but postData has some value I don't know then why it is giving null pointer exception. – Ahmad Shahwaiz Nov 14 '12 at 10:32
  • @AhmadShahwaiz: Please ask a separate SO question, posting your code and stack trace. – Vit Khudenko Nov 14 '12 at 11:29
  • @Archimed I have please this if you can answer this, Thanks. : http://stackoverflow.com/questions/13377021/blackberry-httppost-null-pointer-exception-java – Ahmad Shahwaiz Nov 14 '12 at 12:06
1
 ...
httpConn = (HttpConnection)connDesc.getConnection();    
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("username",name);
httpConn.setRequestProperty("password",pass);
....
oxigen
  • 6,193
  • 3
  • 25
  • 36
  • i got the response code as success but the values didnt get added to the server .. – alanvabraham May 31 '11 at 09:33
  • Use wireShark for check your sended request. If it is correct, than error is in the server side (maybe you need to add User-Agent or sth else..) – oxigen May 31 '11 at 11:43