如何从黑莓本机应用程序调用Web服务

时间:2013-03-13 12:56:25

标签: java web-services blackberry

这是我在Blackberry java Eclipse插件中的代码如何在此代码中调用webservice,通过从此代码调用Web服务,在数据库中存储下面提到的字段,如_firstName,_lastName,address,mobileNumber。 请说一下数据库,如何在黑莓应用程序中设置数据库,以便在黑莓应用程序中调用webservice,即在我的ui设计中我有firstname,lastname,mobilenumebr和地址我必须通过调用将这些字段存储在数据库中webservice,请给我一些关于webservices的想法或代码,并设置特定于blackberry应用程序的数据库。提前致谢

  package mypackage;
    import net.rim.device.api.database.Database;
    import net.rim.device.api.database.DatabaseFactory;
    import net.rim.device.api.database.Statement;
    import net.rim.device.api.io.URI;
    import net.rim.device.api.ui.DrawStyle;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.FieldChangeListener;
    import net.rim.device.api.ui.component.BasicEditField;
    import net.rim.device.api.ui.component.ButtonField;
    import net.rim.device.api.ui.component.DateField;
    import net.rim.device.api.ui.component.Dialog;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.component.RadioButtonField;
    import net.rim.device.api.ui.component.RadioButtonGroup;
    import net.rim.device.api.ui.component.RichTextField;
    import net.rim.device.api.ui.component.SeparatorField;
    import net.rim.device.api.ui.container.HorizontalFieldManager;
    import net.rim.device.api.ui.container.MainScreen;
    import net.rim.device.api.ui.container.VerticalFieldManager;

    public final class CreateProfileScreen extends MainScreen {
        Database d;
        BasicEditField _firstName = new BasicEditField("First Name:", "");

        BasicEditField _lastName = new BasicEditField("Last Name:", "");

        BasicEditField mobileNumber = new BasicEditField("MobileNumber:", "");

        BasicEditField address = new BasicEditField("Address:", "");


        public CreateProfileScreen() {
            HorizontalFieldManager btns = new HorizontalFieldManager(
                    HorizontalFieldManager.FIELD_HCENTER);

            ButtonField save = new ButtonField("Save", ButtonField.FIELD_HCENTER);
            // adding these objects to the Blackberry Application.
            VerticalFieldManager vfm = new VerticalFieldManager(
                    VerticalFieldManager.FIELD_LEFT);
            vfm.add(_firstName);

            vfm.add(new SeparatorField());

            vfm.add(_lastName);
            vfm.add(new SeparatorField());
            vfm.add(mobileNumber);
            vfm.add(new SeparatorField());
            RadioButtonGroup rbg = new RadioButtonGroup();
            vfm.add(new RadioButtonField("Male", rbg, true));
            vfm.add(new RadioButtonField("Female", rbg, false));
            vfm.add(new SeparatorField());

            vfm.add(new DateField("DOB:", System.currentTimeMillis(),
                    DateField.DATE | DrawStyle.LEFT));
            vfm.add(new SeparatorField());
            vfm.add(address);
            vfm.add(new SeparatorField());
            vfm.add(save);

            add(vfm);

            FieldChangeListener btnlistener = new FieldChangeListener() {

                public void fieldChanged(Field field, int context) {
                    // Open a new screen

                    String fname = _firstName.getText();
                    String lname = _lastName.getText();
                    String mobno = mobileNumber.getText();
                    String addr = address.getText();

                    if (fname.length() == 0 || lname.length() == 0)
                        Dialog.alert("firstname and lastname is mandatory!");
                    LabelField title = new LabelField("SQLite Insert Data "
                            + "Schema Sample", LabelField.ELLIPSIS
                            | LabelField.USE_ALL_WIDTH);
                    setTitle(title);
                    add(new RichTextField("Attempting to insert data into "
                            + "MyTestDatabase.db on the SDCard."));
                    try {
                        URI myURI = URI.create("D:///db/" + "MyTestDatabase2.db");
                        d = DatabaseFactory.open(myURI);

                        Statement st = d
                                .createStatement("INSERT INTO People(Name,Age) "
                                        + "VALUES ('John',37)");
                        st.prepare();
                        st.execute();
                        st.close();
                        d.close();
                        add(new RichTextField("inserting data successful"));
                        System.out.println("inserting data successful");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                    }

                }

            };
            save.setChangeListener(btnlistener);
        }

    }

1 个答案:

答案 0 :(得分:0)

试试这个 -

try {

        String httpURL="www.your url.com/register?fname="+fname_value+"&lname="+lname_value+"&mobno ="+no_value+"&addr="+address_value;
        HttpConnection httpConn;
        httpConn = (HttpConnection) Connector.open(httpURL);
        httpConn.setRequestMethod(HttpConnection.POST);
        DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream());
        byte[] request_body = httpURL.getBytes();
        for (int i = 0; i < request_body.length; i++) {
            _outStream.writeByte(request_body[i]);
        }
        DataInputStream _inputStream = new DataInputStream(
        httpConn.openInputStream());
        StringBuffer _responseMessage = new StringBuffer();
        int ch;
        while ((ch = _inputStream.read()) != -1) {
            _responseMessage.append((char) ch);
        }
        String res = (_responseMessage.toString());
        String responce = res.trim();

        Dialog.alert(responce);

        httpConn.close();


    }catch (Exception e) {
        Dialog.alert("Error");
    }