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

[9009 byte] By [Raaghuamateura] at [2007-11-27 4:57:23]
# 1

Hi

Actually u r accessing the Servlet from local System,

http://someservlet/mChekJava/J2MEServlet

, but if u want to use this servlet From phone means , u must have to access that servlet through network , i.e., u have to place this servlet application in public box , then u can access , after that u have to change this url to

http://ipaddress/someservlet/mChekJava/J2MEServlet,

where ipaddress is the address of servlet application in the public box ,

Where public box means it contains some space in internet , like website space , by placing application there we can access through network ,for accessing network u have to check first of all u have network rights to access , else activate it in phone , & for placing application in public box u have to make charges , actually like purchasing space for website.....

Byeeeeee.

vasuvuggu@gmail.coma at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

>>Where public box means it contains some space in internet , like website space , by placing application there we can access through network ,for accessing network u have to check first of all u have network rights to access , else activate it in phone , & for placing application in public box u have to make charges , actually like purchasing space for website.....

>>

I am using proper internet site, where an server is running and all the settings on the phone is proper, if i type the same url on the phone browser explicitly I can see the response. But from the application the request is not hitting the servlet.. Any other suggestion?

Thanks

Raaghu.K

Raaghuamateura at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
have you checked what response code you get?
jonney69a at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4
No no there is no response itself because, the request itself didnt reach servlet
Raaghuamateura at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

Here is one more code from SUN WTK demo, again this works fine on emulator, but not on phone.

THis time I found the exception was when I do

"os.close"

Here is the code

/**

* Read the content of the page. Don't care about the response headers.

*

* @param request

*type of HTTP request (GET or POST)

*/

private void readContents(String request) {

String debugStr = "0";

StringBuffer b = new StringBuffer();

++attempt;

b.append("attempt " + attempt + " content of " + request + " " + url

+ "\n");

HttpConnection c = null;

OutputStream os = null;

InputStream is = null;

TextBox t = null;

try {

long len = -1;

int ch = 0;

long count = 0;

int rc;

DEBUG(request + " Page: " + url);

c = (HttpConnection) Connector.open(url);

debugStr = "2";

DEBUG("c= " + c);

c.setRequestMethod(request);

debugStr = "3";

c.setRequestProperty("foldedField",

"first line\r\n second line\r\n third line");

debugStr = "4";

if (request == HttpConnection.POST) {

debugStr = "5";

String m = "Test POST text.";

debugStr = "6";

DEBUG("Posting: " + m);

os = c.openOutputStream();

debugStr = "7";

os.write(m.getBytes());

debugStr = "8";

//os.flush();

debugStr = "99";

os.close(); // Exception is thrown here

}

debugStr = "9";

rc = c.getResponseCode();

debugStr = "10";

if (rc != HttpConnection.HTTP_OK) {

b.append("Response Code: " + c.getResponseCode() + "\n");

b

.append("Response Message: " + c.getResponseMessage()

+ "\n\n");

}

debugStr = "11";

is = c.openInputStream();

debugStr = "12";

DEBUG("is = " + is);

if (c instanceof HttpConnection) {

len = ((HttpConnection) c).getLength();

}

DEBUG("len = " + len);

if (len != -1) {

// Read exactly Content-Length bytes

DEBUG("Content-Length: " + len);

for (int i = 0; i < len; i++) {

if ((ch = is.read()) != -1) {

if (ch <= ' ') {

ch = ' ';

}

b.append((char) ch);

count++;

if (count > 200) {

break;

}

}

}

} else {

byte data[] = new byte[100];

int n = is.read(data, 0, data.length);

for (int i = 0; i < n; i++) {

ch = data[i] & 0x000000ff;

b.append((char) ch);

}

}

try {

if (is != null) {

is.close();

}

if (c != null) {

c.close();

}

} catch (Exception ce) {

DEBUG("Error closing connection");

}

try {

len = is.available();

DEBUG("Inputstream failed to throw IOException after close");

} catch (IOException io) {

DEBUG("expected IOException (available())");

io.printStackTrace();

// Test to make sure available() is only valid while

// the connection is still open.,

}

t = new TextBox("Http Test", b.toString(), b.length(), 0);

is = null;

c = null;

} catch (IOException ex) {

ex.printStackTrace();

DEBUG(ex.getClass().toString());

DEBUG(ex.toString());

DEBUG("Exception reading from http");

if (c != null) {

try {

String s = null;

if (c instanceof HttpConnection) {

s = ((HttpConnection) c).getResponseMessage();

}

DEBUG(s);

if (s == null)

s = "No Response message";

t = new TextBox("Http Error 1", s, s.length(), 0);

} catch (IOException e) {

e.printStackTrace();

String s = e.toString();

DEBUG(s);

if (s == null)

s = ex.getClass().getName();

s = s + " " + debugStr;

t = new TextBox("Http Error 2", s, s.length(), 0);

}

try {

c.close();

} catch (IOException ioe) {

// do not over throw current exception

}

} else {

t = new TextBox("Http Error 3", "Could not open URL", 128, 0);

}

} catch (IllegalArgumentException ille) {

// Check if an invalid proxy web server was detected.

t = new TextBox("Illegal Argument", ille.getMessage(), 128, 0);

} catch (Exception e) {

t = new TextBox("Error", e.toString(), 128, 0);

}

if (is != null) {

try {

is.close();

} catch (Exception ce) {

;

}

}

if (c != null) {

try {

c.close();

} catch (Exception ce) {

;

}

}

setCommands(t, false);

display.setCurrent(t);

}

Exception was : java.io.Exception: TCP open

Please help me if any one has any clue.

Thanks and Regards

Raaghu.K

Message was edited by:

Raaghuamateur

Raaghuamateura at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
uncomment os.flush() ?
suparenoa at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7
Tried that too, even thats not working :(RegardsRaaghu.K
Raaghuamateura at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8
in which demo of the WTK?
suparenoa at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9
WTK25\apps\Demos\src\example\http\HttpTestThis is the one under WtkRegardsRaaghu.K
Raaghuamateura at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10
which url are you trying to connect?
suparenoa at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 11
Any URL, we tried www.google.com, www.yahoo.com, Any ways the request itself is not going out of the applicaiton
Raaghuamateura at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 12

well I faced the same problem. I have developed http based application. On some devices (nokia,ericssion) it works well but on some devices it goes on connecting mode.

On nokia 6030 (my phone) the request didnt reach upto server so it went on connecting (no exception, no response nothing) only left is to exit the application. But when deployed on another 6030 phone, It worked fine. (I saw the GPRS "G" symbol activated until I exit d application but in my phone it was disappeared so request didnt reach to the server)

check your phone GPRS settings. Open internet and meanwhile run application.

vikaspatela at 2007-7-12 10:12:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...