# 2
Hello,
I'm not able to get an idea from ur code, is there any say if connection is an object of httpconnection then connection.timeout method in j2me or we hav to use the timer in this. If yes i want to know how to do that ? Can u please send me some detail source code if u have >>>>
My code is
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.lang.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;
import java.util.*;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
class Login extends Form implements CommandListener,Runnable
{
public SKFMobi appl;
private TextField Username,Password;
private Command exitCommand;
private Command loginCommand;
private String username,password;
private RecordStore loginRecord,loginRecord2,loginRecord10;
private String routeLocatorURL,message ="";
private HttpConnection connection=null;
private DataInputStream dis = null;
private static Image image = null;
private int ch;
private String StringSeparator="|";
private String SubStringSeparator=",";
private String strSubString ="";
private String strSubSubString ="";
private String firstString="";
private String secondString ="";
public Login(SKFMobi mid)
{
super("....Mobile Application....");
appl=mid;
Username=new TextField("User ID:-", null, 25, TextField.ANY);
Password=new TextField("Password:-", null, 25, TextField.PASSWORD);
loginCommand = new Command("Login",Command.OK,1);
exitCommand = new Command("Exit",Command.EXIT, 1);
try
{
image = Image.createImage("/_single8.png");
}
catch(Exception e)
{
e.printStackTrace();
}
append(Username);
append(Password);
addCommand(loginCommand);
addCommand(exitCommand);
appl.display.setCurrent(this);
setCommandListener(this);
}
public void commandAction(Command command,Displayable displayable)
{
if(command == loginCommand)
{
MyCanvas myCanvas=new MyCanvas();
appl.display.setCurrent(myCanvas);
username=Username.getString();
password=Password.getString();
// System.out.println(password);
BuildURL();
new Thread(this).start();
}
if(command == exitCommand)
{
appl.destroyApp(true);
appl.notifyDestroyed();
}
}
public void run()
{
try
{
establishConnection();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void BuildURL()
{
try
{
loginRecord = RecordStore.openRecordStore("LogInRecord",true);
byte [] UrlByte = loginRecord.getRecord(1);
loginRecord.closeRecordStore();
String url = new String(UrlByte);
routeLocatorURL = "http://"+url+"?aid=01&uid="+username+"&pwd="+password+"";
//System.out.println("for login-->"+routeLocatorURL);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void establishConnection()
{
StringBuffer b = new StringBuffer();
try
{
connection = (HttpConnection)Connector.open(routeLocatorURL);
[u]
/*I hav to check for the connection here and if connection is not then i hav to go in the Menu screen by checkin login and pass in RMS */
[/u]
connection.setRequestMethod(HttpConnection.GET);
int response=connection.getResponseCode();
if(HttpConnection.HTTP_OK == response)
{
dis = new DataInputStream(connection.openInputStream());
try
{
while((ch = dis.read()) != -1)
{
message = message + (char) ch;
}
while(message.length() > 1)
{
strSubString = GetSubString(message,StringSeparator);
firstString = strSubString;
message = GetRemString(message,StringSeparator);
secondString = message;
strSubString="";
}
byte [] SecondByte = secondString.getBytes();
try
{
loginRecord10 = RecordStore.openRecordStore("LogInRecord10",true);
if(loginRecord10.getNextRecordID() == 1)
{
loginRecord10.addRecord(SecondByte,0,SecondByte.length);
}
else
{
loginRecord10.setRecord(1,SecondByte,0,SecondByte.length);
}
loginRecord10.closeRecordStore();
}
catch(Exception e)
{
System.out.println("Record is not written to recordstore");
}
if(firstString.equals("t")==true)
{
addRecord();
Menu menu=new Menu(appl);
}
else
{
Login login = new Login(appl);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
try
{
loginRecord2 = RecordStore.openRecordStore("LogInRecord2",true);
byte [] Uname = loginRecord2.getRecord(1);
byte [] Upass = loginRecord2.getRecord(2);
loginRecord2.closeRecordStore();
String uname = new String(Uname);
String upass = new String(Upass);
//System.out.println(uname);
//System.out.println(upass);
if(uname.equals(username)==true&&upass.equals(password)==true)
{
Menu menu=new Menu(appl);
}
else
{
}
}
catch(Exception f)
{
}
}
}
catch (Exception e)
{
System.out.println("some problem");
e.printStackTrace();
}
}
private String GetSubString(String message,String StringSeparator)
{
String strSubString ="";
int idx=0;
if (message.length() == 0)
return "";
idx=message.indexOf(StringSeparator);
if(idx != -1)
{
strSubString=message.substring(0,idx);
}
else
{
strSubString=message;
}
return strSubString;
}
private String GetRemString(String message,String StringSeparator)
{
String strSubString ="";
int idx=0;
idx=message.indexOf(StringSeparator);
if(idx != -1)
{
strSubString=message.substring(idx+1, message.length());
}
else
{
strSubString="";
}
return strSubString;
}
private void addRecord()
{
byte [] UserName = username.getBytes();
byte [] PassWord = password.getBytes();
try
{
String password=new String(PassWord);
//System.out.println(password);
loginRecord2 = RecordStore.openRecordStore("LogInRecord2",true);
if(loginRecord2.getNextRecordID() == 1)
{
loginRecord2.addRecord(UserName,0,UserName.length);
loginRecord2.addRecord(PassWord,0,PassWord.length);
}
else
{
loginRecord2.setRecord(1,UserName,0,UserName.length);
loginRecord2.setRecord(2,PassWord,0,PassWord.length);
}
loginRecord2.closeRecordStore();
}
catch(Exception e)
{
System.out.println("Record is not written to recordstore");
}
}
}
class MyCanvas extends Canvas implements Runnable
{
Graphics g;
int x=17,y=35,k;
Font f1 = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_ITALIC,Font.SIZE_LARGE);
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0x000000);
g.drawRect(10,10,getWidth()-20,getHeight()/2);
g.fillRect(15,(getHeight()/2)+10,getWidth()-20,5);
g.fillRect(getWidth()-10,15,5,getHeight()/2);
g.setColor(0xff00ff);
g.setFont(f1);
g.drawString(" PLEASE WAIT ",10,10,0);
g.setColor(0x4b0082);
g.fillRoundRect(15,33,getWidth()-35,10,7,7);
g.setColor(0x87cefa);
new Thread(this).start();
g.fillRoundRect(x,y,getWidth()/8,6,7,7);
}
public void run()
{
try
{
Thread.sleep(30);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
k=getWidth()-50;
if(x>k)
{
x=12;
}
else
{
x=x+5;
}
repaint();
}
}
* Pleas see the bold and underline part in the code
thnkx in advance.........
# 13
>
>I have tried ur code but i am not able to do it properly
>
Have u tried to do it in a proper way ? I can't see any reflection of this in ur code !!!
However, I'm showing u the way to do it here. But remember I have done it in blind way, means I haven't even compiled the code snippet. So if any such kind of problem arise, u got to solve that ur self.
Here u go....
if(command == loginCommand)
{
// MyCanvas myCanvas=new MyCanvas();
// appl.display.setCurrent(myCanvas);
username=Username.getString();
password=Password.getString();
// System.out.println(password);
BuildURL();
new Thread(this).start();
}
public void run()
{
try
{
establishConnection();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void establishConnection() // also bit edited....
{
try
{
connection = (HttpConnection)Connector.open(routeLocatorURL);
//added here....
// connection time out timer should ideally start after the connection has established.
timer.schedule(mTT, timeout) ;
dis = new DataInputStream(connection.openInputStream());
while((ch = dis.read()) != -1)
{
message = message + (char) ch;
//System.out.println("message"+message);
}
// edited here.....
// reached here means connection has successfully made and response has reached.
setConnectionNotEstablished(false);
}
catch(Exception e)
{
e.printStackTrace();
}
finally{ // added block...
try{
if(dis != null)
dis.close();
} catch (Exception e) {
}
try{
if(connection != null)
connection.close();
} catch (Exception e) {
}
}
}
Timer timer = new Timer ();
MyTimerTask mTT = new MyTimerTask ();
boolean connectionNotEstablished = true;
private class MyTimerTask extends TimerTask {
public final void run() {
if (connectionNotEstablished) {
// reached here means connection has timed out..
// close the connections here
try{
if(dis != null)
dis.close();
} catch (Exception e) {
}
try{
if(connection != null)
connection.close();
} catch (Exception e) {
}
// TODO task scheduled to be done if connection timed out.
}
}
If this doesn't work, so am I....
Regds,
SD