Passing parameter from Midlet to Servlet

S

Saaji

Hi ppl,
I'm trying to pass a parameter from my MIDlet code which is
running via OTA on to the servlet which is running on my localhost
using the HTTPConnection.GET method. The MIDlet execution goes on till
connection.getResponseCode() and it doesn't show any exception. So I
surmised that it is sending the data..

But my Servlet says that it cannot get the parameters and returns a
null..

Could u tell me what is wrong?

I'm using my Java Wireless Toolkit to run the MIDlet using the OTA
provision.

Thanks and Regards,
Sajida
 
D

Darryl L. Pierce

Saaji said:
I'm trying to pass a parameter from my MIDlet code which is
running via OTA on to the servlet which is running on my localhost
using the HTTPConnection.GET method. The MIDlet execution goes on till
connection.getResponseCode() and it doesn't show any exception. So I
surmised that it is sending the data..

But my Servlet says that it cannot get the parameters and returns a
null..

Could u tell me what is wrong?

Post your code from the MIDlet that's opening the connection, et. al.

--
Darryl L. Pierce <[email protected]>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
S

Saaji

Here it is:
My MIDlet keeps listening for a message and when it receives it, it
sends it off to the servlet...

private void initListener(final String connectionString)
{
Thread t = new Thread()
{
public void run()
{
try
{
MessageConnection listener = (MessageConnection)
Connector.open(connectionString);
try
{
while (!shutdown)
{

System.out.println("listener reading at "+connectionString);
Message msg = listener.receive();
if (msg instanceof TextMessage)
{
System.out.println("Message Received:
"+((TextMessage)msg).getPayloadText());
str = ((TextMessage)msg).getPayloadText();
String url = "http://localhost:8080/api-eBay/servlet/apiEbay"+
"?" + "message=" + str + "&" + "done=" + "yes";
System.out.println("The string is: "+ str);
sendHttpGetRequest(url);
//sendHttpPostRequest("http://localhost/api-eBay/servlet/apiEbay",
str); appendData(((TextMessage)msg).getPayloadText());
}
}
}
finally
{
listener.close();
}
} catch (IOException x)
{
x.printStackTrace();
}
}
};
t.start();
}

public static String sendHttpGetRequest(String url) throws IOException
{
HttpConnection connection=null;
InputStream is=null;
OutputStream os=null;
StringBuffer buffer=new StringBuffer();
System.out.println("With HTTPGetRequest");

int response=0;
try
{
// open the URL and set up the connection for POSTing.
connection=(HttpConnection)Connector.open(url);
System.out.println("Opened connector with url");

// set the request headers.
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("User-Agent", "Profile/MIDP 2.0
Configuration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-US");
System.out.println("Set all request properties");
// now get the response and test it
response = connection.getResponseCode();
System.out.println("Got response");
if(response!=HttpConnection.HTTP_OK)
{
throw new IOException("Http response code:"+response);
}
System.out.println("the response is:"+response);

// open the stream
is=connection.openInputStream();

int chars;
//read input and store in the buffer
while ((chars=is.read())!=-1)
{
buffer.append((char)chars);
}
System.out.println("what is seen here after response:"+ chars);
System.out.println("what is seen here after buffer
appending:"+buffer.toString());
}
catch(Exception e)
{
buffer.setLength(0);
buffer.append(POST_STATUS_FAILED);
}
finally
{
try
{
if(is!=null)
{
is.close();
}
if(connection!=null)
{
connection.close();
}
}
catch (Exception ignoreException) {}
}

return buffer.toString();
}

Please send a reply as soon as possible:(

Thanks in Advance,
Sajida
 
D

Darryl L. Pierce

Saaji said:
Here it is:
My MIDlet keeps listening for a message and when it receives it, it
sends it off to the servlet...
<snip>

If you enter this URL into your browser:

http://localhost:8080/api-eBay/servlet/apiEbay?message=foo&done=yes

Does the servlet do what you're expecting?

--
Darryl L. Pierce <[email protected]>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
S

Saaji

Hi darryl,
My MIDlet code worked when I used the POST method and removed
os.flush. But now I'm facing a new problem. My servlet / JSP page
(depending on where I'm passing the parameter to) displays the value on
the Tomcat console as long as I give a sysout on that page. But when I
try accessing it from my browser, it returns a value of null. Is there
some way in which I can make this value which i'm getting once persist
till I get another value from the MIDlet?
I'm using a request.getParameter() from my JSP and a
request.getReader().readLine() from my Servlet.
Thanks in Advance,
Sajida
 
D

Darryl L. Pierce

Saaji said:
Hi darryl,
My MIDlet code worked when I used the POST method and removed
os.flush.

I didn't notice that in the code before. Don't flush the output stream: that
causes the data to get munged.
But now I'm facing a new problem. My servlet / JSP page
(depending on where I'm passing the parameter to) displays the value on
the Tomcat console as long as I give a sysout on that page. But when I
try accessing it from my browser, it returns a value of null. Is there
some way in which I can make this value which i'm getting once persist
till I get another value from the MIDlet?
I'm using a request.getParameter() from my JSP and a
request.getReader().readLine() from my Servlet.

You need to have some kind of object that's shared between sessions that can
pass that value back and forth.

--
Darryl L. Pierce <[email protected]>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
S

Saaji

The parameter I'm geting from the MIDlet is the location where the
mobile is.. I want to retrieve this info from my wap site. Is there any
way of doing this?

Thanks in Advance,
Sajida
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top