import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.bwie.myadapters.MyBaseAdapter;
import com.google.gson.Gson;
import java.util.ArrayList;
import myutils.NetWorkUtils;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private ArrayList<News> news;
private MyBaseAdapter myBaseAdapter;
public final static String JSON_URL = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
//请求数据,配置数据
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
String json_url = params[0];//得到数据url
//网络请求,得到json数据
String str = new NetWorkUtils().getJsonByUrlConnection(json_url);
return str;//还回结果
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//得到请求的json数据,并解析
SuperClass superClass = new Gson().fromJson(s,SuperClass.class);
news = superClass.getNewslist();
//创建适配器
myBaseAdapter = new MyBaseAdapter(news, MainActivity.this);
lv.setAdapter(myBaseAdapter);
}
}.execute(JSON_URL);
}
}
package com.bwie.listviewmoreitem;
/**
* Created by Administrator on 2017/9/7.
*/
public class News {
private String ctime;
private String title;
private String description;
private String picUrl;
private String url;
public News(String ctime, String title, String description, String picUrl, String url) {
this.ctime = ctime;
this.title = title;
this.description = description;
this.picUrl = picUrl;
this.url = url;
}
public String getCtime() {
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "News{" +
"ctime='" + ctime + '\'' +
", title='" + title + '\'' +
", description='" + description + '\'' +
", picUrl='" + picUrl + '\'' +
", url='" + url + '\'' +
'}';
}
}
package com.bwie.listviewmoreitem;
import java.util.ArrayList;
public class SuperClass {
private int code;
private String msg;
private ArrayList<News> newslist;
public SuperClass(String msg, ArrayList<News> newslist, int code) {
this.msg = msg;
this.newslist = newslist;
this.code = code;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ArrayList<News> getNewslist() {
return newslist;
}
public void setNewslist(ArrayList<News> newslist) {
this.newslist = newslist;
}
@Override
public String toString() {
return "SuperClass{" +
"code=" + code +
", msg='" + msg + '\'' +
", newslist=" + newslist +
'}';
}
}