4

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.

Shruti
  • 1
  • 13
  • 55
  • 95
Richard
  • 4,516
  • 11
  • 60
  • 87
  • actually, the platform for the Nokia 5800 is Series60 FIFTH edition but that doesn't matter for your question – michael aubert Jul 30 '09 at 18:58
  • yes, you are right, except I could only find a sdk third edition. I am not sure what to do with it yet, but I will find out. – Richard Jul 31 '09 at 14:26
  • The newer Series60 SDK do both C++ and j2me. go to http://www.forum.nokia.com/Tools_Docs_and_Code/Tools/Platforms/S60_Platform_SDKs/ then "Download All-in-one S60 SDKs" – michael aubert Oct 08 '09 at 14:16

2 Answers2

9

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.

jhauberg
  • 1,410
  • 12
  • 20
  • 1
    any chance JSR172 does the trick without requiring an additional library? – michael aubert Jul 30 '09 at 18:59
  • @JAcob, thanks for your input. I had more or less given up already on this post. This is very usefull. Now, at least I have a startingpoint. Just have to figure out wich library's I have to reference on top off the file. Also how to use the sdk with netbeans. Anyway, lots to figure out. – Richard Jul 31 '09 at 14:37
  • 1
    @Jacob please do you think you could help me with this question http://stackoverflow.com/questions/23696897/how-to-consume-asp-net-web-api-from-a-java-me-application – Axel May 17 '14 at 12:50
  • @Jacob do you have an example of how to parse the xml?? – eddy May 17 '14 at 17:28
2

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.