Connecting your J2ME App to the internet: Part two


In the previous tutorial we were able to generate the web services needed so that we can log in to our app hosted on an online server. In this tutorial we are going to create a J2ME parser that will consume the web services.
Step one
Create a J2ME project using your favorite editor. I prefer Netbeans for all my J2ME development. In Netbeans you do so by going to File>>New Project>>Java ME
Give your project a name, I have named mine J2MEInternet, you can use any name you want to. Set the project to be the main project but don’t generate a helloworld.
I like organizing my code in packages depending on what the code does. I have therefore created a package specifically for the parser. To do the same right click on your project select New>>Java Package and then give it a name. You can use any name but please remember to follow Java conventions, package names start with lower case letters.
Step two
Now that we have our project structure we can get to what we really wanted to do; create a parser. However before we do that let me explain the approach that I use. The advantage of using OOP languages is that you can have everything as objects. I use this approach for all my coding. I am going to create a class that will contain all the user details. In the parser I am going to populate this class and then refer to it my J2ME code. Let us then dive head fast into it, if you haven’t understood my approach I hope you will understand it as you do it.
Right click on the package we created and create a new Java class. In Netbeans it is New>>Java Class. Name the class Person to reflect the information that it is going to hold. When we log in we provide a username and a password. If the credentials match we get the following information
•    User id
•    Username
•    Name
•    Email
We will store this information in our class. Declare the above as global variables in the class Person but set the access modifier as private since we are going to have setters and getters for them. I have made all my variables to be strings as all values returned by our web service are strings.
Step three
In the class Person set getters and setters for all our variables. The final class should look as follows
public class Person {

private String userid;
private String username;
private String name;
private String email;
public void setUserid(String userid){
this.userid=userid;
}
public String getUserid(){
return userid;
}
public void setUsername(String username){
this.username=username;
}
public String getUsername(){
return username;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setEmail(String email){
this.email=email;
}
public String getEmail(){
return email;
}
}

You can download this tutorial in pdf format here
What next?
In the next tutorial I am going to introduce the KXML Parser and show you how to use it in J2ME to parse XML. There are many parsers out there but I found KXML Parser to be comprehensive and stable.

Advertisements

2 thoughts on “Connecting your J2ME App to the internet: Part two

    • Java converts the application source code to byte code. I am not really sure if you can reverse this byte code to source code so unless the software author avails the source code there is no way you can access it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s