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

Ric*_*ard 4 xml rest web-services java-me

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

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

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

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

唯一的东西,我发现是基于肥皂的.

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

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

jha*_*erg 9

要调用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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