Android从Json Feed创建String []

时间:2012-05-16 10:15:28

标签: android json string

我有一个惰性列表,在URL []中硬编码的时刻加载到图像列表中,我想通过json feed加载它们。所以我的问题是如何从JsonObject创建一个字符串[]?

到目前为止,我已经得到了什么

try {
        post.setEntity (new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();
        fulldata = String.valueOf(json);
        Log.v("myApp","newsdata" + fulldata);

        newsList = new ArrayList<String>();
        newsList2 = new ArrayList<String>();
        newsList3 = new ArrayList<String>();



        JSONObject obj = new JSONObject(json);    
        JSONObject objData = obj.getJSONObject("data");
        JSONArray jArray = objData.getJSONArray("news");


           for(int t = 0; t < newsAmount; t++){
               JSONObject newsTitleDict = jArray.getJSONObject(t);

//this is where i want to load the images into a String[]
JSONArray ImageArray = objData.getJSONArray("news");

             newsList3.add(newsTitleDict.getString("title"));

           }

           for(int t = 0; t < 1; t++){
               JSONObject newsTitleDict = jArray.getJSONObject(t);

             newsList.add(newsTitleDict.getString("title"));
             newsList2.add(newsTitleDict.getString("title"));

           }



    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




    arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
    arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
    //arrayAdapter3 = new ArrayAdapter(this, R.layout.complex_item, newsList3);



     String[] mStrings={
             "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
             "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
             "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
             "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
             "http://a1.twimg.com/profile_images/97470808/icon_normal.png",


     };



     arrayAdapter3 = new LazyAdapter(this, mStrings);


        ListView list = getListView();
           list.setTextFilterEnabled(true);

           LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
            View header = inflater.inflate( R.layout.homeheader, list, false);
            View header2 = inflater.inflate( R.layout.homeheader2, list, false);
            View header3 = inflater.inflate( R.layout.homeheader3, list, false);



        adapter = new MergeAdapter();
        adapter.addView(header);
        adapter.addAdapter(arrayAdapter);
        adapter.addView(header2);
        adapter.addAdapter(arrayAdapter2);
        adapter.addView(header3);
        adapter.addAdapter(arrayAdapter3);
        setListAdapter(adapter);

    }   

1 个答案:

答案 0 :(得分:0)

这是我使用JSONObject中的ArrayList向String []添加值的方法。我的示例使用了JSONArray,但您可以随时更改它以适合您的代码。

ArrayList<String> arrayName = new ArrayList<String>();
ArrayList<String> arrayPicture = new ArrayList<String>();

Bundle extras = getIntent().getExtras();
apiResponse = extras.getString("API_RESPONSE");

try {

    JSONArray JAFriends = new JSONArray(apiResponse);

    for (int i = 0; i < JAFriends.length(); i++) {
        json_data = JAFriends.getJSONObject(i);


        if (json_data.has("name"))  {
            String getFriendName = json_data.getString("name").toUpperCase();
            arrayName.add(getFriendName);
        } else {
            String getFriendName = null;
            arrayName.add(getFriendName);
        }       

        if (json_data.has("pic_square"))    {
            String getFriendPhoto = json_data.getString("pic_square");
            arrayPicture.add(getFriendPhoto);
        } else {
            String getFriendPhoto = null;
            arrayPicture.add(getFriendPhoto);
        }
    }
} catch (JSONException e) {
    return;
}
stringName = new String[arrayName.size()];
stringName = arrayName.toArray(stringName);

stringPicture = new String[arrayPicture.size()];
stringPicture = arrayPicture.toArray(stringPicture);

listofFriends = (ListView)findViewById(R.id.list);
adapter = new FriendsAdapter(this, stringName, stringPicture);

listofFriends.setAdapter(adapter);

for循环中,使用 if 语句确保没有错误蔓延,返回的值将添加到各自的ArrayLists和adapter之前的6行代码中设置后,ArrayLists中的值将添加到String []中。 希望这有助于解决您的问题。