j2me:推荐的显示大量结果的方法 [英] j2me: Recommended way to display a considerable number of results

查看:131
本文介绍了j2me:推荐的显示大量结果的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你是否了解显示来自WebService的数据(可能以表格格式)的实用方法.

I want to ask you if you know of practical way to display data (maybe in tabular format) coming from a WebService.

我真的是j2me的新手,我刚刚学习了如何使用RESTful Web服务,现在我需要学习如何向用户显示数据(json格式).

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.

您将如何在j2me中显示典型的json数组?当您必须处理大量结果时,如何处理内存有限和屏幕尺寸过小的限制?

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.

谢谢.

P.S. 这是罐子我曾经使用过json.

P.S. This is the jar I used to work with json.

推荐答案

This link will definitely help you dealing with JSONArray.

要谈论内存限制,您只需尝试清理不需要任何有关UI的资源/数据结构,只需保持当前屏幕的实例处于活动状态(如果可能),而无需堆栈中的其他实例.如果您的数组包含许多您一次无法显示的对象(这可能会导致类似OOM的问题),那么您就可以想到分页了.

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.

这篇关于j2me:推荐的显示大量结果的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆