Richard
Richard

Reputation: 4546

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.

Upvotes: 4

Views: 3739

Answers (2)

xphree
xphree

Reputation:

I'm working with REST and with JSONObject's and JSONArrays with this library for Mobile Ajax in Java ME. Is an easy approach working on REST, the library need to be downloaded in source code format and compiled with Netbeans or ANT, it's easy, just check out the project via SVN and do a build in netbeans (WithXML or WithoutXML), in the page are samples using REST and public web services like Yahoo Maps! I hope you enjoy it.

Upvotes: 2

jhauberg
jhauberg

Reputation: 1430

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.

Upvotes: 9

Related Questions