适用于Eclipse的BlackBerry Java插件 - .net Web服务

时间:2012-06-24 13:44:38

标签: blackberry

我有一个接受用户名和密码的Web服务,如果登录凭据正确,它会返回用户的姓名,状态,图像和最后已知的gps坐标。

至于现在,我被困在“登录”按钮中,应用程序既不会继续也不会引发任何错误。模拟器没有产生任何结果,我无法将应用程序加载到我的手机上。

package mypackage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.util.Hashtable;

import javacard.framework.UserException;

import javax.microedition.io.HttpConnection;
import javax.microedition.location.Location;
import javax.microedition.location.LocationProvider;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.component.pane.TitleView;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.image.Image;

public class LoginTest extends UiApplication 
{
   public static void main(String[] args)  
   {
      //Create a new instance of the app
      //and start the app on the event thread.
      LoginTest app = new LoginTest();
      app.enterEventDispatcher();
   }

   public LoginTest()
   {
      //Display a new screen.
      pushScreen(new LoginTestScreen());
   }
}

//Create a new screen that extends MainScreen and provides
//behaviour similar to that of other apps.
final class LoginTestScreen extends MainScreen
{
   //declare variables for later use
   private InfoScreen _infoScreen;
   private ObjectChoiceField choiceField;
   private int select;
   BasicEditField username;
   PasswordEditField passwd;
   CheckboxField checkBox1;
   ButtonField loginBtn;
   Hashtable persistentHashtable;    
   PersistentObject persistentObject;
   static final long KEY = 0x9df9f961bc6d6baL;
   // private static final String URL="http://prerel.track24elms.com/Android/T24AndroidLogin.asmx";
   String strResult;

   public LoginTestScreen()
   {
      //Invoke the MainScreen constructor.
      super();

      //Add a screen title.
      setTitle("Track24ELMS");

      LabelField login = new LabelField("ELMS Login", LabelField.FIELD_HCENTER); 
      login.setFont(Font.getDefault().derive(Font.BOLD, 30));
      login.setMargin(10, 0, 20, 0); //To leave some space from top and bottom

      HorizontalFieldManager user = new HorizontalFieldManager();
      user.setMargin(0, 0, 10, 0);
      HorizontalFieldManager pass = new HorizontalFieldManager();
      pass.setMargin(0, 0, 20, 0);
      HorizontalFieldManager checkbox = new HorizontalFieldManager();
      checkbox.setMargin(0, 0, 30, 0);
      HorizontalFieldManager btns = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);

      LabelField usernameTxt = new LabelField("Username :");
      LabelField passwordTxt = new LabelField("Password :");

      username = new BasicEditField();
      passwd = new PasswordEditField();

      loginBtn = new ButtonField("Login", ButtonField.CONSUME_CLICK);
      // btn.setChangeListener(new view listener);

      //checkBox1 = new CheckboxField("Remember me", false,Field.FOCUSABLE);
      checkBox1 = new CheckboxField("Remember me",false);

      user.add(usernameTxt);
      user.add(username);
      pass.add(passwordTxt);
      pass.add(passwd);
      //checkbox.add(checkBox1);
      btns.add(loginBtn);
      add(login);
      add(user);
      add(pass);
      add(checkBox1);
      add(btns);

      // loginBtn.setChangeListener(btnlistener);
   }

   public void saveChecked() {
      persistentHashtable.put("", username.getText());
      persistentHashtable.put("", passwd.getText());
      persistentHashtable.put("BoolData", new Boolean(checkBox1.getChecked()));
      persistentObject.commit();
   }

   FieldChangeListener btnlistener = new FieldChangeListener() {

      public void fieldChanged(Field field, int context) {
         //Open a new screen
         String uname = username.getText();
         String pwd = passwd.getText();

         //If there is no input
         if (uname.length() == 0 || pwd.length()==0) {
            Dialog.alert("One of the textfield is empty!");
         } else {
            final String METHOD_NAME = "ValidateCredentials";
            final String NAMESPACE = "http://tempuri.org/";
            final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
            final String URL = "http://prerel.track24elms.com/Android/T24AndroidLogin.asmx";

            SoapObject resultRequestSOAP = null;
            HttpConnection httpConn = null;
            HttpTransport httpt;
            System.out.println("The username" + uname + "password" + pwd );
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //String usernamecode = Base64.encode(username.getBytes());
            //String pwdEncodeString = Base64.encode(passwd.getBytes());
            request.addProperty("Username", "abc");//First parameter is tag name provided by web service
            request.addProperty("Password", "xyz");
            System.out.println("The request is=======" + request.toString());
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.bodyOut = request;
            envelope.dotNet = true;
            envelope.encodingStyle = SoapSerializationEnvelope.XSD;
            envelope.setOutputSoapObject(request);
            System.out.println("The envelope has the value++++"+ envelope.toString());

            /* URL+  Here you can add paramter so that you can run on device,simulator etc. this will work only for wifi */
            httpt = new HttpTransport(URL+ ";deviceside=true;ConnectionUID=S TCP-WiFi");
            httpt.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            httpt.debug = true;
            try
            {
               System.out.println("SOAP_ACTION    ==   " + SOAP_ACTION);
               httpt.call(SOAP_ACTION, envelope);
               System.out.println("the tranport" + httpt.toString());
               resultRequestSOAP = (SoapObject) envelope.bodyIn;
               System.out.println("result    ==   " + resultRequestSOAP);            
            }
            catch (IOException e) {
               System.out.println("The exception is IO==" + e.getMessage());
            } catch (XmlPullParserException e) {
               System.out.println("The exception xml parser example==="
                     + e.getMessage());
            }
            System.out.println( resultRequestSOAP);

            UiApplication.getUiApplication().pushScreen(new InfoScreen()); //Open a new Screen
         }
      }
   };

   //To display a dialog box when a BlackBerry device user
   //closes the app, override the onClose() method.
   public boolean onClose()
   {
      if(checkBox1.equals("true"))
      {
         persistentObject = PersistentStore.getPersistentObject(KEY);

         if (persistentObject.getContents() == null) {
            persistentHashtable = new Hashtable();
            persistentObject.setContents(persistentHashtable);
         }
         else {
            persistentHashtable = (Hashtable)persistentObject.getContents();
         }

         if (persistentHashtable.containsKey("EditData")) {
            username.setText((String)persistentHashtable.get("EditData"));
         }

         if (persistentHashtable.containsKey("BoolData")) {
            Boolean booleanObject = (Boolean)persistentHashtable.get("BoolData");
            checkBox1.setChecked(booleanObject.booleanValue());
            if(booleanObject.booleanValue()==true){
               saveChecked();
            }
         }
      }
      Dialog.alert("Goodbye!");
      System.exit(0);
      return true;
   }

   //Create a menu item for BlackBerry device users to click to see more 
   //information about the city they select.

   private MenuItem _viewItem = new MenuItem("More Info", 110, 10) 
   {
      public void run() 
      {
         //Store the index of the city the BlackBerry device user selects
         select = choiceField.getSelectedIndex();

         //Display a new screen with information about the
         //city the BlackBerry device user selects
         _infoScreen = new InfoScreen();
         UiApplication.getUiApplication().pushScreen(_infoScreen);
      }
   };

   //Create a menu item for BlackBerry device users to click to close 
   //the app.
   private MenuItem _closeItem = new MenuItem("Close", 200000, 10) 
   {
      public void run()
      {
         onClose();
      }
   };

   //To add menu items to the menu of the app, 
   //override the makeMenu method.


   //Create an inner class for a new screen that displays
   //information about the city a BlackBerry device user selects.
   private class InfoScreen extends MainScreen
   {
      public InfoScreen() 
      {
         super();
         setTitle("Itinerary");

         LabelField login = new LabelField("Employee Itinerary", LabelField.FIELD_HCENTER); 

         Bitmap bitmap = Bitmap.getBitmapResource("img1.jpg");
         EditField statusMsg = new EditField("Status Message", "Update status here");
      }
   }
}

1 个答案:

答案 0 :(得分:1)

在您发布的代码中,没有任何设置可以响应您按下的登录按钮。

首先,让我们删除实现FieldChangeListener匿名类:

FieldChangeListener btnlistener = new FieldChangeListener() { 
  public void fieldChanged(Field field, int context) { 

并且像这样:

private class LoginButtonListener implements FieldChangeListener {
  public void fieldChanged(Field field, int context) {
    // no change to the content of this method!
  }
}

并在LoginTestScreen的构造函数中实例化它,并将其挂钩到登录按钮:

loginBtn = new ButtonField("Login", ButtonField.CONSUME_CLICK); 
loginBtn.setChangeListener(new LoginButtonListener());
在注释掉的代码中,它看起来很接近。只需要一点点。试试看,并报告回来!

注意:可以使其与您最初拥有的匿名按钮侦听器类一起使用。我只是不喜欢匿名类的可读性,特别是因为你的btnListener成员被声明的地方完全不同于其他所有成员。真正缺失的部分是对setChangeListener的呼唤。我只是想根据需要区分我推荐的内容。