Search code examples
javaeclipseblackberryblackberry-simulatorblackberry-eclipse-plugin

The different ways of accessing the internet to perform POST in BlackBerry


I am making an application to perform an POST action in it . Which is shown below

//private boolean x = false;
url = "http://domain.com/login";
InputStream inputStream = null;
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
    HttpConnection httpConn;
    httpConn = (HttpConnection)connDesc.getConnection();
    httpConn.setRequestMethod(HttpConnection.POST);
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);

    encPostData.append("username",username);
    encPostData.append("password",password);

    byte[] postData = encPostData.toString().getBytes("UTF-8");
    httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));

    httpConn.openOutputStream().write(encPostData.getBytes()); 

    try
    {
        int Response = httpConn.getResponseCode();
        System.out.println("Response Code = "+Response+httpConn.getResponseCode());
        if (Response == 200)
        {
            System.out.println("Response Code = "+Response+httpConn.getResponseCode());
            inputStream = httpConn.openInputStream();
            byte[] responseData = new byte[10000];
            int length = 0;
            StringBuffer rawResponse = new StringBuffer(10000);
            while (-1 != (length = inputStream.read(responseData)))
            {
                rawResponse.append(new String(responseData, 0, length));
            }
            replyMessage = rawResponse.toString();
            key = replyMessage.substring(12, replyMessage.length()-2);
        }
        else if(Response == 500)
        {
            Dialog.alert("User Details Incorrect");
        }
        else
        {
            System.out.println(Response);
        }
    }
    catch (IOException e)
    {
        Dialog.alert("Connection not established");
    }
}
else
{
    Dialog.alert("Connection not established");
}
return key;

i am now wondering under which type of service would this work. I want to make my application work if there be no gprs/ 3g but have a wifi connection .. Or the other way around too ..

Is there anyways to identify first the networks available n later pick a network for communication.

I appreciate any help in this regard..

Thank You,


Solution

  • Take some time to read TransportInfo javadoc