Irwan Harianto L
Irwan Harianto L

Reputation: 9

How to make method POST in Blackberry

I want to login to my Blackberryapp, but this app still in method GET, I want to change this app into method POST. Because my server using method POST. This is my source code in my BB app.

    package com.blackberry.mobile_banking;

import java.io.InputStream;
import java.io.InputStreamReader;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.blackberry.api.browser.URLEncodedPostData;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class Check_Login {

    HttpConnection httpconnection;
    InputStream inputstream;

    public Check_Login()
    {
    }
    public void validasi_Login(String username, String password)
    {
        try {
            URLEncodedPostData params=new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, true);
            params.append("username", username);
            params.append("password", password);
            String url="http://127.0.0.1:80/proyek_akhir/cek_login.php?"+params.toString()+";deviceside=true";
            System.out.println(url);

            //connect to server

            httpconnection=(HttpConnection)Connector.open(url);
            inputstream=httpconnection.openDataInputStream();

            if(httpconnection.getResponseCode()==HttpConnection.HTTP_OK)
            {
                InputStreamReader reader=new InputStreamReader(inputstream, "UTF-8");
                int readCharacter;
                StringBuffer responseBuffer=new StringBuffer();

                while((readCharacter=reader.read())!=-1)
                {
                    responseBuffer.append((char)readCharacter);

                }
                if(responseBuffer.toString().equalsIgnoreCase("SUCCESS"))
                {
                    Screen_Home sc_home=new Screen_Home();
                    UiApplication.getUiApplication().pushScreen(sc_home);
                }
                else if(responseBuffer.toString().equalsIgnoreCase("FAILED"))
                {
                    Dialog.alert("Username or Password wrong");

                }
            }

        } catch (Exception e) {

        }

    }

}

and this my source code in my server

  <?php
    require('db.php');
    $username=$_POST['username'];
    $password=md5($_POST['password']);
    $query="select*from user where username='$username' and password='$password'";
    $hasil=mysql_query($query);
    if(mysql_num_rows($hasil)>0)
    {
        echo "SUCCESS";
    }
    else
    {
        echo "FAILED";
    }
?>

Upvotes: 1

Views: 195

Answers (3)

Kevin
Kevin

Reputation: 1626

I literally finished my POST method yesterday. This answer helped me a lot, you should be able to use it almost as is:

Blackberry jde : how to upload an image in server using MultipartPostData

Upvotes: 0

Mister Smith
Mister Smith

Reputation: 28158

Unless your app were a MIDlet, or a legacy pre-5.0 application, I'd use the newer ConnectionFactory class to avoid problems with the transport types (BES, BIS, Wi-Fi, etc). Back in the old days we had to append a different suffix for each transport type to the URL. You can avoid that creating a new factory, which you can configure once and reuse in other parts of the code:

        ConnectionFactory factory = new ConnectionFactory();
        factory.setPreferredTransportTypes(<array of TransportInfo.TRANSPORT_XXX >);
        factory.setTransportTypeOptions(<options>); //Only required for BIS
        ... 

Then each time you need a connection you obtain the HttpConnection object like this:

        ConnectionDescriptor cd = factory.getConnection(<url>);
        HttpConnection httpConn = (HttpConnection) cd.getConnection();
        httpConn.setRequestMethod(HttpConnection.POST);
        //Now proceed as normal

The request method as you can see is configured in each new HttpConnection instance and has nothing to do with the factory.

Upvotes: 1

Rince Thomas
Rince Thomas

Reputation: 4158

try this -

httpconnection=(HttpConnection)Connector.open(url);
httpconnection.setRequestMethod(HttpConnection.POST);//this will use post method

Upvotes: 0

Related Questions