如何在Blackberry中制作方法POST

时间:2013-07-05 06:49:34

标签: post blackberry http-post blackberry-simulator blackberry-eclipse-plugin

我想登录我的Blackberryapp,但这个应用程序仍然在方法GET,我想将此应用程序更改为方法POST。因为我的服务器使用POST方法。 这是我的BB应用程序中的源代码。

    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) {

        }

    }

}

这是我服务器中的源代码

  <?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";
    }
?>

3 个答案:

答案 0 :(得分:1)

除非您的应用是MIDlet或旧版5.0之前的应用,否则我会使用较新的ConnectionFactory类来避免传输类型(BES,BIS,Wi-Fi等)出现问题。回到过去,我们不得不为URL的每种传输类型添加不同的后缀。您可以避免创建新工厂,您可以配置一次并在代码的其他部分重用:

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

然后,每次需要连接时,都会获得如下的HttpConnection对象:

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

您可以看到的请求方法在每个新的HttpConnection实例中配置,与工厂无关。

答案 1 :(得分:0)

试试这个 -

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

答案 2 :(得分:0)

我昨天完成了我的POST方法。这个答案对我有很大的帮助,你应该可以按原样使用几乎

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