0

can someone please help me, i am relatively new in J2ME coding.... i have an API link which contains username and password (e.g. http://sunday-tech.com/chunghua/api/login.php?username=yasir&password=yasir )

i am trying to authenticate the user inputs and access the API and read the data. I am using httpconnection but i am not sure if its the right one to use.

so far the codes do access and display the contents by reading each character but how do i EXCLUDE commas and brackets as well display the data in an organized manner in the phone. Also, when the username and password inputs are wrong, it should alert Invalid Login......

I would appreciate it so much if someone can enlighten about this by providing some code snippets. here is wht i have done so far.

public class Login extends MIDlet implements CommandListener {

TextField UserName = null;
TextField Password = null;
Form authForm, mainscreen;
TextBox t = null;
StringBuffer b = new StringBuffer();
private Display myDisplay = null;
private Command okCommand = new Command("OK", Command.OK, 1);
private Command exitCommand = new Command("Exit", Command.EXIT, 2);
private Command backCommand = new Command("Back", Command.BACK, 2);
private Alert alert = null;

public Login() {

    myDisplay = Display.getDisplay(this);

    UserName = new TextField("Username", "", 10, TextField.ANY);
    Password = new TextField("Password", "", 10, TextField.ANY);
    authForm = new Form("Identification");
    mainscreen = new Form("Logging IN");
    mainscreen.append("Logging in....");
    mainscreen.addCommand(backCommand);
    authForm.append(UserName);
    authForm.append(Password);
    authForm.addCommand(okCommand);
    authForm.addCommand(exitCommand);
    authForm.setCommandListener(this);
    myDisplay.setCurrent(authForm);

}

public void startApp() throws MIDletStateChangeException {
}

public void pauseApp() {
}

protected void destroyApp(boolean unconditional)
        throws MIDletStateChangeException {
}

public void commandAction(Command c, Displayable d) {

 if ((c == okCommand) && (d == authForm)) {
 if (UserName.getString().equals("") || Password.getString().equals("")) {
 alert = new Alert("Error", "You should enter Username and Password", null, AlertType.ERROR);
   alert.setTimeout(Alert.FOREVER);
   myDisplay.setCurrent(alert);
        } 

   else {
            //myDisplay.setCurrent(mainscreen);
            login(UserName.getString(), Password.getString());
        }
    }
    if ((c == backCommand) && (d == mainscreen)) {
        myDisplay.setCurrent(authForm);
    }
    if ((c == exitCommand) && (d == authForm)) {
        notifyDestroyed();
    }
}

public void login(String UserName, String PassWord) {
    HttpConnection connection = null;
    DataInputStream in = null;
    String base = "http://sunday-tech.com/chunghua/api/login.php";
    String url = base + "?username=" + UserName + "&password=" + PassWord;

    OutputStream out = null;
    try {
        connection = (HttpConnection) Connector.open(url);
        connection.setRequestMethod(HttpConnection.POST);
        connection.setRequestProperty("IF-Modified-Since", "2 Oct 2002                                     15:10:15 GMT");
        connection.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.0");
        connection.setRequestProperty("Content-Language", "en-CA");
        connection.setRequestProperty("Content-Length", "" + (UserName.length() + PassWord.length()));
        connection.setRequestProperty("UserName", UserName);
        connection.setRequestProperty("PassWord", PassWord);
        out = connection.openDataOutputStream();
        out.flush();
        in = connection.openDataInputStream();
        int ch;
        while ((ch = in.read()) != -1) {
            b.append((char) ch);
            //System.out.println((char)ch);
        }
        //t = new TextBox("Reply",b.toString(),1024,0);
        mainscreen.append(b.toString());
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
        if (connection != null) {
            connection.close();
        }
    } catch (IOException x) {
    }
    myDisplay.setCurrent(mainscreen);

}
}

0 Answers0