Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MIDlet cann't connect to servlet on website

 
Bander Al-Mehmadi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guyz,

I'm new in this field, also in the saloon!, i'm working on a simple MIDlet that connect to a servlet on test website (stream in & out simple text), but the problem that the MIDlet enters a waiting state & never finishes!!

I test it localy on tomcat Server it works fine, Also there is no problem in my Internet connection,SO? is there any kind of different setting between a MIDlet talking to a Local servlet (on tomcat)OR Remote Servlet(on website)?

here my whole code for the MIDlet:

public class FlightSchedualMIDlet extends MIDlet implements CommandListener
{
Display d;
String data = null;
Form input;

TextField date;
TextField from;
TextField to;

String url = "http://almehmadi.eatj.com/FlightSchedual/FlightSchedualServlet";// Remote

//String url = "http://localhost:8080/examples/servlet/FlightSchedualServlet"; // Local

static final Command ok = new Command("OK", Command.OK, 2);
static final Command back = new Command("Back", Command.BACK, 3);
static final Command exit = new Command("Exit", Command.STOP, 1);

/**
* Constructor for TestMIDlet.
*/
public FlightSchedualMIDlet()
{
d = Display.getDisplay(this);
}

/**
* @see MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException
{
mainMenu();
}

/**
* @see MIDlet#pauseApp()
*/
protected void pauseApp()
{
}

/**
* @see MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean flag) throws MIDletStateChangeException
{
}

void mainMenu()
{
input = new Form("**** Flight Schedual ****");

date = new TextField("Date: (ddmmyyyy)", null, 8, TextField.NUMERIC);

from = new TextField("From:", null, 3, TextField.ANY);

to = new TextField("To:", null, 3, TextField.ANY);

input.append(date);
input.append(from);
input.append(to);
input.addCommand(ok);
input.addCommand(exit);

input.setCommandListener(this);

d.setCurrent(input);
}

void invokeServlet(String url) throws IOException
{
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer sb = new StringBuffer();
Form resultForm = null;
StringItem stringItem = new StringItem(null, "");

Form waitForm = new Form("Waiting..."); // while waitting for data to come
d.setCurrent(waitForm);

data = "";
data = data + date.getString();
data = data + from.getString();
data = data + to.getString();

try
{
c = (HttpConnection) Connector.open(url);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");

//Send Out Data

os = c.openOutputStream();

byte postmsg[] = data.getBytes();

for (int i = 0; i < postmsg.length; i++)
{
os.write(postmsg[i]);
System.out.print(postmsg[i]);// print out data sent
}

os.flush();


//Receive In Data

is = c.openInputStream();

int ch;

while ((ch = is.read()) != -1)
{
sb.append((char) ch);

}

System.out.print(sb.toString());// print out data received

stringItem.setText(sb.toString());// cast results from sb to StringItem to append it on the output Form


// Screen Out Data Received

resultForm = new Form("**** Flight Schedual ****");
resultForm.append(stringItem);
resultForm.addCommand(back);
resultForm.addCommand(exit);
resultForm.setCommandListener(this);

}
finally
{
if (is != null)
{
is.close();
}
if (os != null)
{
os.close();
}
if (c != null)
{
c.close();
}
}


d.setCurrent(resultForm);
}

public void commandAction(Command c, Displayable d)
{

String label = c.getLabel();


if (label.equals("Exit"))
{
notifyDestroyed();;
}

else if (label.equals("Back"))
{
mainMenu();
}

else if (label.equals("OK"))
{

Thread t = new Thread()
{
public void run()
{
try
{
invokeServlet(url);
}
catch (IOException e)
{
}
}
};
t.start();


}



}
}
 
Francis Lee Mondia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi....

I'm just wonderin if you have tried it using post. i cant seem to get this method to work. i want to pass data to a servlet, username and password for database authentication. GET works fine but i guess alot of people are having problem with the POST method.

As for your project, i think the problem might be with your URL. Check the type of remote server and match url format with the server type. Tomcat and J2EE servers are very different u know. I'm testing that here and that is usually the problem. I'm testing on remote tomcat and j2ee servers.

Hope this helps and that you can help me also....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic