how to display fetched json data into listview using baseadapter

前端 未结 7 824
不知归路
不知归路 2020-11-29 07:22

I am new to android and java.Recently I am having problem with displaying fetched json data into listview using baseadapter. At first I have used this code

          


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 07:32

    In this example, I am connecting to Twitters public timeline JSON url.

    package net.inchoo.demo.andy1;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    
    public class HomeActivity extends ListActivity {
    
        /** Called when the activity is first created. */
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fetchTwitterPublicTimeline()));        
        }
    
        public ArrayList fetchTwitterPublicTimeline()
        {
            ArrayList listItems = new ArrayList();
    
            try {
                URL twitter = new URL(
                        "http://twitter.com/statuses/public_timeline.json");
                URLConnection tc = twitter.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        tc.getInputStream()));
    
                String line;
                while ((line = in.readLine()) != null) {
                    JSONArray ja = new JSONArray(line);
    
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = (JSONObject) ja.get(i);
                        listItems.add(jo.getString("text"));
                    }
                }
            } catch (MalformedURLException 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();
            }
            return listItems;
        }
    }
    

    Please direct your attention on the listItems.add(jo.getString(“text”)); line. This is the part that I am grabbing a “text” attribute/property of single JSON object. To get a more “visual” picture of all available attributes/properties you might want to take a look at the XML version of twitters public timeline. This way you will get nice colored XML in your browser, where you can see all available attributes.

    Link: http://inchoo.net/dev-talk/android-development/simple-android-json-parsing-example-with-output-into-listactivity/

提交回复
热议问题