xml – 使用JME(或J2ME)使用REST服务

我需要一些帮助才能开始这个.

我需要知道如何调用REST服务并解析xml.

我的PHP脚本只返回一些xmlcode,没有别的.(没有wsdl或uddi)

诺基亚5800的平台是S60第三版(将适用)
诺基亚sdk是同名的.
我为这个项目安装了Netbeans.

唯一的东西,我发现是soap based.

我可以使用哪些方法/库?

我必须提到我也是java / netbeans的新手.

解决方法

调用REST Web服务,您可以使用httpconnection类:

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();
    }
}

现在响应将包含您的服务器吐出的XML.

然后,您可以使用kXML2库来解析它.请注意,此库引用了XMLPull库,因此您必须将其包含在项目中.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念