Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Passing parameter from Midlet to Servlet

Thread view: 
Saaji - 09 Mar 2006 11:22 GMT
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
Darryl L. Pierce - 09 Mar 2006 16:30 GMT
>       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
[quoted text clipped - 6 lines]
>
> Could u tell me what is wrong?

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

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart

Saaji - 10 Mar 2006 06:50 GMT
    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
Darryl L. Pierce - 10 Mar 2006 15:49 GMT
> 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?

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart

Saaji - 14 Mar 2006 10:26 GMT
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
Darryl L. Pierce - 14 Mar 2006 16:21 GMT
> 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
[quoted text clipped - 4 lines]
> 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.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart

Saaji - 14 Mar 2006 16:26 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.