【问题标题】:Android create a String[] from a Json FeedAndroid 从 Json Feed 创建一个 String[]
【发布时间】:2012-05-16 10:15:27
【问题描述】:

我有一个惰性列表加载图像列表,此时 url 被硬编码在 String[] 中,我想通过 json 提要加载它们。所以我的问题是如何从 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);

    }   

【问题讨论】:

    标签: android json string


    【解决方案1】:

    这就是我使用 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[]。 希望这有助于解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      相关资源
      最近更新 更多