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