Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume REST service with JME (or J2ME)

I need some help getting started with this.

I need to know how to call the REST service and parse the xml.

My php script only sends back some xmlcode, nothing else.(no wsdl or uddi)

The platform for the Nokia 5800 is S60 third edition.(that one will apply) The Nokia sdk go's by the same name. I installed Netbeans for this project.

The only stuff, that I find are soap based.

What methods/library's are at my disposal for this?

I have to mention that I am also new to java/netbeans.

like image 514
Richard Avatar asked Dec 22 '22 10:12

Richard


1 Answers

To call the REST webservice you can use the HttpConnection class:

HttpConnection connection = null;
InputStream is = null;

final ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] response = null;

try {
    connection = (HttpConnection)Connector.open("http://api.yourserver.com/rest/things/12", Connector.READ);
    connection.setRequestMethod(HttpConnection.GET);

    connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");

    if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
        is = connection.openInputStream();

        if (is != null) {
            int ch = -1;

            while ((ch = is.read()) != -1) {
                bos.write(ch);
            }

            response = bos.toByteArray();
        }
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (bos != null) {
            bos.close();
            bos = null;
        }

        if (is != null) {
            is.close();
            is = null;
        }

        if (connection != null) {
            connection.close();
            connection = null;
        }
    } catch (Exception e2) {
        e2.printStackTrace();
    }
}

Now response will contain the XML your server spat out.

You can then use the kXML2 library to parse it. Note that this library references the XMLPull library, so you have to include that in your project.

like image 106
jhauberg Avatar answered Dec 25 '22 12:12

jhauberg