logo       

Re: Web services and J2ME: msg#00235

java.sun.kvm

Subject: Re: Web services and J2ME

> Thanks again for your answer. They are running MIcrosoft servers, trying
to
> do everything .NET. They don't use Java at all and would rather stay all
> microsoft. Where could I find some info about how to write the server
extension that
> would parse the POST and then communicate with their Web Service? Will we
> then be able to call this extension from the Midlet with an URL like we
call the
> servlet on Tomcat?

Absolutely. When you open an HttpConnection in MIDP, you're effectively
getting access to a raw HTTP 1.1 request. Open it as a POST request, not a
GET request, to give yourself flexibility in what you can pass. You'll also
want to set the "Content-Type" header appropriately. If all the arguments
to your web service can be safely passed as strings, you'll find that the
"application/x-www-form-urlencoded" type is what you want. Then you can
pass the parameters the same way that an HTML form does. For example, to
pass parameter "arg1" with value "foo" and "arg2" with value "blart", you
could do:

String url = .....
HttpConnection conn = null;
OutputStream os = null;

try {
conn = (HttpConnection) Connector.open( url );
conn.setRequestMethod( HttpConnection.POST );
conn.setRequestProperty( "Content-Type",
"application/x-www-form-urlencoded" );

os = conn.openOutputStream();

String data = "arg1=foo&arg2=blart";
os.write( data.getBytes() );

int rc = conn.getResponseCode();

..... etc. etc.
}
catch( IOException e ){
}

Now the above is a very simple example. If your strings have any reserved
characters in them (like spaces), you'll need to encode them. The example
also assumes that you're only dealing with ASCII characters. If you can do
it this way, though, you'll be able to easily test your server-side code
just by writing a page of HTML that POSTs the necessary data.

The other alternative is to use the "application/octet-stream" MIME type and
send your own binary stream of data. If you were sending data to a Java
servlet then this would be particularly trivial, because you could use
DataInputStream and DataOutputStream to read and write the data. You can
still use DataOutputStream to write the data, but you'll need to understand
how DataOutputStream stores the data and read it out correctly from the ASP
page on the server.

The key to all of this working is that you have a good understanding of how
HTTP works and how to use it from MIDP. This is a shameless plug, but you
should look at Chapter 6 of my and Enrique's MIDP book (see
http://www.ericgiguere.com/books/midp/index.html) for a good explanation of
HTTP and MIDP along with some sample code.

Finding sample ASP code for parsing POST requests and for making web service
calls should be simple enough using a few Google searches....

Eric

===========================================================================
To unsubscribe, send email to listserv@xxxxxxxxxxxx and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@xxxxxxxxxxxx and include in the body of the message "help".



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise