问题
I want to ask you if you know of practical way to display data (maybe in tabular format) coming from a WebService.
I'm really new to j2me, I've just learned how to consume RESTful webservices, now I need to learn how show that data (which comes in json format) to the user.
Basically this is the code I use:
protected String jsonParse(String url) {
StringBuffer stringBuffer = new StringBuffer();
InputStream is = null;
HttpConnection hc = null;
System.out.println(url);
String Results = null;
try {
mProgressString.setText("Connecting...");
hc = (HttpConnection) Connector.open(url, Connector.READ);
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.1");
if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
is = hc.openInputStream();
int ch;
while ((ch = is.read()) != -1) {
stringBuffer.append((char) ch);
}
}
} catch (SecurityException se) {
System.out.println("Security Excepction: " + se);
} catch (NullPointerException npe) {
System.out.println("Null Pointer Excepction: " + npe);
} catch (IOException ioe) {
System.out.println("IO Exception" + ioe);
}
try {
hc.close();
is.close();
} catch (Exception e) {
System.out.println("Error in MostActivePareser Connection close:" + e);
e.printStackTrace();
}
String jsonData = stringBuffer.toString();
try {
JSONArray js = new JSONArray(jsonData);
System.out.println(js.length());
mProgressString.setText("Reading...");
String Results = "";
for (int i = 0; i < js.length(); i++) {
JSONObject jsObj = js.getJSONObject(i);
Results += "-----------------------------------\n";
Results += jsObj.getString("Name") + " \n";
Results += jsObj.getString("Phone") + " \n";
Results += jsObj.getString("Address");
}
} catch (JSONException e1) {
System.out.println("Json Data error:" + e1);
e1.printStackTrace();
} catch (NullPointerException e) {
System.out.println("null error:" + e);
}
return Results;
}
As you can see I only concatenating the results to then show it to the user.But surely there's gotta be better ways to that and here's where I need you help.
How would you display a typical json Array in j2me ?? How do you deal with limitations such as limited memory and small screen sizes when you have to work with a considerable number of results?
As always, any advice or resources you could provide would be greatly appreciated.
Thanks in advance.
P.S. This is the jar I used to work with json.
回答1:
This link will definitely help you dealing with JSONArray.
To talk about memory limitation,you just try to clean up the resources/data structures that you don't need any more means about UI just keep instance of current screen alive (if possible) no need of other instances in stack.If your array contains many objects that you can't display at a one time (as can cause OOM like issues),then you can think of Pagination.
You can get better idea about Memory Optimization from this must see presentation.
来源:https://stackoverflow.com/questions/23860325/j2me-recommended-way-to-display-a-considerable-number-of-results