JSONArray解数组,三秒更新

public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";

public String urltext ="https://www.toutiao.com/hot_words/";
private ListView lv;
private TextView tv;
private Adpater adp;
private String ggg="";
int le=-1;
List<String> lists = new ArrayList<>();

Handler h=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        JSONArray array = null;
        try {
            array = new JSONArray(ggg);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        for (int i = 0;i<array.length();i++){
            lists.add(array.optString(i));
        }
       tv.setText(lists.get(le%lists.size()));
    }
};
private String getstring;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = findViewById(R.id.lv);
    tv = findViewById(R.id.tv);

    new AsyncTask<String,Integer,String>(){
        @Override
        protected String doInBackground(String... strings) {
            String client = Utils.getClient(urlString);
            return client;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson=new Gson();
            List<Att.DataBean> data = gson.fromJson(s, Att.class).getData();
            adp = new Adpater(MainActivity.this,data);
            lv.setAdapter( adp);
        }
    }.execute();

    new Thread(){
        @Override
        public void run() {
            super.run();

            ggg = Utils.getClient(urltext);

            while(true){
                try {
                sleep(3000);
                h.sendEmptyMessage(le);
                le++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();

}

猜你喜欢

转载自blog.csdn.net/xude01/article/details/78919753