Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Server response for login button in blackberry

I am trying to capture the server response from the login button. If the login is accepted the server sends back the response which is in xml where the first part will contain "credentialaccepted" followed on by the user's name, image and last status update. What I am looking from the 'login' button is to take the response, split it, check if it has "credentials accepted" and if it does show the rest of the returned message (name,image,status) onto the new screen. This is the first BlackBerry app that I am working on and literally struggling. Would appreciate some help. Thanks

 

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

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);
loginBtn.setChangeListener(new LoginButtonListener());

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

}

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


private class LoginButtonListener implements 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", "Sarah.Farukh");//First parameter is tag name provided by web service
request.addProperty("Password", "shahrukh");
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) {
// TODO Auto-generated catch block
System.out.println("The exception is IO==" + e.getMessage());
} catch (XmlPullParserException e) {

// TODO Auto-generated catch block
System.out.println("The exception xml parser example==="
+ e.getMessage());
}
System.out.println( resultRequestSOAP);
if(resultRequestSOAP.equals("credentialaccepted"))
{
UiApplication.getUiApplication().pushScreen(new InfoScreen()); //Open a new Screen
}
else
{
Dialog.alert("Login details not valid");
}
}
}

};

//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.


//connect to webservice

/* public static SoapObject LoginUsers(String userName, String password)throws UserException, ClassCastException
{
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 user678 name" + userName.toString()+ "password" + password);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String usernamecode = Base64.encode(userName.getBytes());
String pwdEncodeString = Base64.encode(password.getBytes());
request.addProperty("strEmail", "sarah.farukh");//First parameter is tag name provided by web service
request.addProperty("strPassword", pwdEncodeString);
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) {
// TODO Auto-generated catch block
System.out.println("The exception is IO==" + e.getMessage());
} catch (XmlPullParserException e) {

// TODO Auto-generated catch block
System.out.println("The exception xml parser example==="
+ e.getMessage());
}
return resultRequestSOAP;
}
*/
//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");

}
}
}


Please use plain text.
Developer
simon_hain
Posts: 15,842
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Server response for login button in blackberry

when working with soap i can strongly suggest to get it working with J2SE first, for example by using JUnit (or just as an application).
When you use ksoap you can just use HttpTransportSE instead of the client version.

regarding the BB specifics: don't do networking on the event thread, see
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: Server response for login button in blackberry

Hi,

 

Thanks for the reply. I am new to BlackBerry development and I might not know the basics. I am trying to understand as I proceed. Are you suggesting that it is difficult to read the server response through ksoap? If I apply HttpTransportSE, how will I be reading the server response. Please explain with sample code for a better understanding.

Please use plain text.
Developer
simon_hain
Posts: 15,842
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Server response for login button in blackberry

what i suggest is to develop the webservice client entirely in desktop java, as ksoap also works there.
instead of launching the simulator you get instant results, and much better stacktraces etc.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: Server response for login button in blackberry

I am actually doing this BB application that I have to submit next week and probably wouldnt have time for that. Webservice client in desktop java wouldnt work on BlackBerry and I would need more time for it. I am hoping to resolve the server response issue that I am currently facing.
Please use plain text.
Developer
simon_hain
Posts: 15,842
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Server response for login button in blackberry

sure, do whatever you want :-)
my webservice code runs on both bb java and desktop java, and the only thing i had to pull out was the connection (httptransportSE vs httptransport).

regarding server response, be aware that ksoap cannot parse http error responses, you would have to add that by overwriting serviceconnectionmidp.

if you cannot wrap your head around using desktop java it may be helpful to use tcpmon as a proxy between your application and the service, and soapUI to doublecheck how a correct request should look like.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: Server response for login button in blackberry

Thanks Simon for the pointers. I will look into it. I am already struggling so might as well try your approach and see what I can get.

Please use plain text.
Developer
simon_hain
Posts: 15,842
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Server response for login button in blackberry

i have a buildTransport method that pulls the transport constructor out of the platform independent webservice.
the j2se impl is very simple:
return new HttpTransportSE(url);

my j2se project has a dependency on the bb project. now you only have to take care not to use any bb-api specific code in the webservice classes. if you need anything platform dependent externalize it using an interface, i did that with a logger service, for example.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.