I want to ask you if you know about a practical way to display data (maybe in tabular format) from WebService.
I am really new to j2me, I just learned how to consume RESTful web services, now I need to learn how to show this data (which are in json format) to the user.
This is basically the code that 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 concatenate the results so that I can show it to the user later. But, of course, there should be better ways, and here, where I need you, you will 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 significant amount of results?
As always, any advice or resources you could provide would be greatly appreciated.
Thanks in advance.
PS This is jar. I worked with json.
eddy source share