Working Fine on Emulator, but not working on phone
Hi,
I am developing one J2ME application which communicates with the servlet (http), The application was working fine on emulator but it is not working on Phone (Samsung/Nokia). The request itsef did not hit the servlet
So I used one of the sample code, which is there in this forum, still the same problem persists, Any clue?
/**
* Examples using HttpConnection.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* sample http example MIDlet.
*/
publicclass HttpExampleextends MIDletimplements CommandListener{
Display display =null;
List menu =null;
TextBox input =null;
String user =null;
String url ="http://someservlet/mChekJava/J2MEServlet";
staticfinal Command backCommand =new Command("Back", Command.BACK, 0);
staticfinal Command submitCommand =new Command("Submit", Command.OK, 2);
staticfinal Command exitCommand =new Command("Exit", Command.STOP, 3);
String currentMenu =null;
public HttpExample(){
String temp = readJadAttribute("URL");
url = temp ==null ? url : temp;
}
String readJadAttribute(String value){
String temp = getAppProperty(value).trim();
if (temp !=null && temp.length() > 0)
return temp;
returnnull;
}
publicvoid startApp()throws MIDletStateChangeException{
display = Display.getDisplay(this);
/*menu = new List("Invoke Servlet",
Choice.IMPLICIT); menu.append("Add a user", null);
menu.addCommand(exitCommand); menu.setCommandListener(this);
mainMenu();*/
addName();
}
publicvoid pauseApp(){
}
publicvoid destroyApp(boolean unconditional){
notifyDestroyed();
}
void mainMenu(){
display.setCurrent(menu);
}
// Prompt the user to input his/her name.
publicvoid addName(){
input =new TextBox("Enter anything:","", 5, TextField.ANY);
input.addCommand(submitCommand);
input.addCommand(backCommand);
input.addCommand(exitCommand);
input.setCommandListener(this);
input.setString("");
display.setCurrent(input);
}
// Prepare connection and streams invoke servlet.
void invokeServlet(final String url)throws IOException{
new Thread (){
publicvoid run(){
HttpConnection c =null;
InputStream is =null;
OutputStream os =null;
StringBuffer b =new StringBuffer();
TextBox t =null;
try{
c = (HttpConnection) Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
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 request to the servlet.
os = c.openOutputStream();
String str ="name=" + user;
byte postmsg[] = str.getBytes();
System.out.println("Length: " + str.getBytes());
os.close();
is = c.openInputStream();
int ch;
// receive response and display it in a textbox.
while ((ch = is.read()) != -1){
b.append((char) ch);
}
t =new TextBox("First Servlet", b.toString(), 1024, 0);
display.setCurrent(t);
}catch (Exception e){
System.out.println("Exception");
}
}
}.start();
}
publicvoid commandAction(Command arg0, Displayable arg1){
// TODO Auto-generated method stub
if (arg0 == submitCommand){
try{
invokeServlet(url);
}catch (IOException e){
// TODO Auto-generated catch block
System.out.println("IO Exception");
e.printStackTrace();
}
}
elseif (arg0 == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
Thanks and Regards
Raaghu.K

