Connection problem

hello,

I want to know about http connection. Actually i want the code for the connection timeout in j2me.I want to check for say 1000 ms for connection and if connection is not there then i want to display some other screen

Please help me out this problem.Please send me some source code........

thanks in advance

[339 byte] By [anup123a] at [2007-11-27 0:14:43]
# 1

> I want to check for say 1000 ms for connection and if

> connection is not there then i want to display some

> other screen

> Please help me out this problem.Please send me some

> source code........

....

public void checkIfTimeOutIsReallyOut(){

if(timeout > 1000){

displayAlert("timeout is out!");

}

}

...

public void displayAlert(String s){

// display an Alert with the String parameter

}

[url #" style="display: block; background-image: url('http://www.echecsetmaths.com/atelier/gifs/smiley/mf_type.gif' ); width: 41px; height: 17px] [/url]

suparenoa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 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.........

anup123a at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
Oops, its a big code...Watch this tips.. this might help you... http://www.java-tips.org/java-me-tips/midp/how-to-ping-network-connection-on-j2me-device.htmlCheers,SD
find_suvro@SDNa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4
I have already seen that site. Would you please give me any source code u have of connection time out or may be give me some idea to check for the connection.
anup123a at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5
this article http://today.java.net/pub/a/today/2006/01/24/introduction-to-blackberry-j2me.htmlmay help you...it is for blackberry but you can easily change it to use it for your application
suparenoa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6

Well the idea is to use some TimerTask or generate a thread that will check for the time out.

And if it reaches the certain limit, close the connection and show whatever you choose to show.

How effectively it can be done that depends on your logic and implementation.

Regds,

SD

Message was edited by:

find_suvro@SDN

find_suvro@SDNa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7
hi, anup plz check http api's for time out
ParagPatila at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8

Hello,

Thanks for ur replies. But what happening is when i use the timer and if the connection is not there it will throws an exception and goes into the catch. And in j2me for connection we have to use try catch . So please tell me what to do now>

Cause it is very imp to me, according to our clients they need their application to run offline also and for doing that i need to check connection and go further to the next screens.

Thanks in advance.......

anup123a at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9

Do it like this -->

Timer timer = new Timer ();

MyTimerTask mTT = new MyTimerTask ();

boolean connectionNotEstablished = true;

private class MyTimerTask extends TimerTask {

public final void run() {

// your body goes here

if (connectionNotEstablished) {

// close all opened streams and the connection if they are not null...

callOfflineTask();

}

}

private void makeConnection (String url) throws IOException {

try {

// here goes ur connection establishment

timer.schedule(mTT, timeout) ; // ur desired timeout.

// and the tasks afterwards..

setConnectionNotEstablished(false); // if connection establishes.

// some other tasks.

} catch (Exception e) {

throw new IOException(e.getMessage());

} finally {

// close all opened streams and the connection if they are not null...

}

}

public void run () {

try {

makeConnection (URL);

}

catch (IOException ioe) {

mTT.cancel();

callOfflineTask();

}

}

Hope it work.

Regds,

SD

find_suvro@SDNa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10
Hey Thanks for your help. I am trying your code
anup123a at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 11
Always welcome.SD
find_suvro@SDNa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 12

Hello,

I have tried ur code but i am not able to do it properly i am sending my code can u just help and explain it in my code plz its imp

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

{

try

{

connection = (HttpConnection)Connector.open(routeLocatorURL);

dis = new DataInputStream(connection.openInputStream());

}

catch(Exception e)

{

}

try

{

while((ch = dis.read()) != -1)

{

message = message + (char) ch;

//System.out.println("message"+message);

}

}

catch(Exception e)

{

e.printStackTrace();

}

}

anup123a at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 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

find_suvro@SDNa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 14
why don't you create a specific thread like find@.. told you?and to understand how threads work, you MUST read this articles: http://developers.sun.com/techtopics/mobility/midp/articles/threading/ http://developers.sun.com/techtopics/mobility/midp/articles/threading2/
suparenoa at 2007-7-11 22:00:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 15
Hi guys,Thanks for ur help. I am trying the way you told Once again thanks for the helpRegards,Anup
anup123a at 2007-7-21 19:40:37 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 16
Hey man it worked Thanks for ur help................ Thanks again.............Regards, Anup
anup123a at 2007-7-21 19:40:37 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 17
Welcome..Regds,SD
find_suvro@SDNa at 2007-7-21 19:40:37 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 18

Hello ,

I have done with the time out problem now what i have to do, I am storing my url in the rms if i am offline. So what i have to do is when i got the connection available i have to first send the url which is stored in the rms or first check in the RMS for any URL stored if yes send it first amd then do the new transaction.

So firstly i want to ask is it possible and second we have to use the synchronised method for that or any other way is there ?

Please help me in this................

Thanks in advance..............

anup123a at 2007-7-21 19:40:37 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 19

> Hello ,

> I have done with the time out problem now

> what i have to do, I am storing my url in the rms if

> i am offline. So what i have to do is when i got the

> connection available i have to first send the url

> which is stored in the rms or first check in the RMS

> for any URL stored if yes send it first amd then do

> the new transaction.

> So firstly i want to ask is it possible and

The answer to this query is in the link I provided in my first post to this thread.

> second we have to use the synchronised method for

> that or any other way is there ?

Depends upon how u r implementing ur logic. But if u don't understand synchronization of threads well, its better avoid that. Because this some times arouse more problem if not implemented in a structured manner. How ever u will find some good examples about synchronization in the tutorials provided by SUN.

>Please help me in this................

> Thanks in advance..............

find_suvro@SDNa at 2007-7-21 19:40:37 > top of Java-index,Java Mobility Forums,Java ME Technologies...