public class ChildFragment1 extends Fragment{
//接口网址
private String titleUrl = "https://www.toutiao.com/hot_words/";private ImageView iv;
private TextView tv;
//定义一个集合
ArrayList<String> titleList = new ArrayList<>();int flag = 0;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if (what ==1 ){
if(titleList !=null){
int position = flag%titleList.size();
tv.setText(titleList.get(position));
}
flag++;
//定义延迟发送的方法
sendMsg();}
}
};
//重写AsyncTask的方法
private class MTitleAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String netJson = NetUtil.getNetJson(strings[0]);
return netJson;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//解析Json
try {
JSONArray jsonArray = new JSONArray(s);
for (int i = 0; i < jsonArray.length(); i++) {
Log.e("wzq", "onPostExecute: "+jsonArray.getString(i) );
titleList.add(jsonArray.getString(i));
}
sendMsg();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private void sendMsg() {
//延迟发送消息;
handler.sendEmptyMessageDelayed(1,3000);
}