English 中文(简体)
• 如何获得json string的价值,然后通过这一职能加以分配
原标题:how to get the value of json string and then parse it through this function

我有以下方法,连接干b,返回一只小.。

private String getServerData(String returnString) {

       InputStream is = null;

       String result = "";
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("year","1970"));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(KEY_121);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "
");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                   // JSONObject json_data = jArray.getJSONObject(i);
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","id: "+json_data.getInt("id")+
                            ", name: "+json_data.getString("name")+
                            ", sex: "+json_data.getInt("sex")+
                            ", birthyear: "+json_data.getInt("birthyear")
                    );


                        //Get an output to the screen
                        returnString += "
	" + jArray.getJSONObject(i); 
                }
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());

                returnString += result.toString();
        }
        return returnString; 
    }   

然后,我想把这种方法从外部说来,但又说是很新的,以 j灭,我不敢肯定,如何把它放在像评论那样的hm中。 见以下守则:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listView = (ListView)findViewById(R.id.list);
        myBooks = new ArrayList<HashMap<String,Object>>();
        HashMap<String, Object> hm;
        hm = new HashMap<String, Object>();
        List<String> list=new ArrayList<String>();
        list.add(getServerData(KEY_121));





        //With the help of HashMap add Key, Values of Book, like name,price and icon path 
       /* hm = new HashMap<String, Object>();
        hm.put(BOOKKEY, "Android");
        hm.put(PRICEKEY, "Price Rs: 500");
        hm.put(IMGKEY, R.raw.android); //i have images in res/raw folder

        myBooks.add(hm);


 SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listbox, 
                new String[]{BOOKKEY,PRICEKEY,IMGKEY}, new int[]{R.id.text1, R.id.text2, R.id.img});

        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    }

我在这里的反对是,利用时标,就像在评论守则中所做的那样,获得回击,并把它添加到我的布ook中。

增 编

最佳回答

由于getServerData(>>)在执行期间(包含一项http://www.un.org/Depts/dp.htm)无法直接从国歌手操作。

这意味着你必须创建<><>AsyncTask>>,并打电话getServerData( from the doInBackground()。 您可以回馈展示的JSON结果。 缩略语

这意味着,你希望通过<http://download.oracle.com/javase/1.4.2/docs/api/java/lang/ref/SoftReference.html>rel=“nofollow”>/SoftReference,其中你希望向表明其结果。 AsyncTask。 如果出现这样的情况,你希望将其编为<代码>SoftReference,而此时,你重新召集了JSON(如你脱离活动)。

我建议你读到this blog article,并用the Code from the article <>>。 一旦到任,将这一图像下载者改为一名JSON下载者。 该条应当对你真正有益,因为它还利用改编者在清单中展示物品。

你的榜样以几种方式不同于该条,但我认为,这实际上比较简单,这样,如果你在该条的法典上把头包起来,你就能够接手你的法典。

References:
AsyncTask
SoftReferences
Multithreading for Performance
Working Code from the Article Above which implements something like you want but w images

问题回答

here is what I did and it works just fine

页: 1

2 之后,我建造了一座楼梯,然后,我就能够把它同这同起来。

 returnedResult = this.getServerData(KEY_121);
       try {
        JSONArray jArray = new JSONArray(returnedResult);
        for(int i=0;i<jArray.length();i++){
            // JSONObject json_data = jArray.getJSONObject(i);
            JSONObject json_data = jArray.getJSONObject(i);

            hm = new HashMap<String, Object>();
            hm.put(IDKEY, json_data.getInt("id"));
            hm.put(NAMEKEY, json_data.getString("name"));
            hm.put(IMGKEY, R.raw.android);
            myBooks.add(hm);

        }

Hope that helps to someone here thanks





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签