Download JSON Lib For J2ME
|
J2ME - Parse JSON from PHP |
HttpConnection hc = null;
InputStream ic = null;
try {
hc = (HttpConnection) Connector.open("http://localhost/test/json.php");
hc.setRequestMethod("GET");
ic = hc.openInputStream();
int contentLength = (int)hc.getLength();
byte [] b = new byte[contentLength];
ic.read(b);
String response = new String(b);
JSONObject object = new JSONObject(response);
response = object.getString("time");
System.out.println("Time: " + response);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (hc != null) {
hc.close();
ic.close();
}
} catch (Exception e) {}
}
json.php
echo json_encode(array("time" => strval(date("H:i:s"))));