先寫權限<uses-permission android:name=“android.permission.INTERNET” /
lic class MainActivity extends AppCompatActivity {
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
ArrayList<Demo.T1348649079062Bean> list1 = (ArrayList<Demo.T1348649079062Bean>) msg.obj;
//将解析到的資料添加到集合中
list.addAll(list1);
//重新整理擴充卡
adapter.notifyDataSetChanged();
}
};
private ListView li;
private ArrayList<Demo.T1348649079062Bean> list;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//連結網絡
list = new ArrayList<>();
adapter = new MyAdapter(list, MainActivity.this);
li.setAdapter(adapter);
new Thread() {
@Override
public void run() {
try {
URL url = new URL("http://c.m.163.com/nc/article/list/T1348649079062/0-20.html");
HttpURLConnection op = (HttpURLConnection) url.openConnection();
int code = op.getResponseCode();
//判斷是否連接配接網絡
if (code == 200) {
InputStream in = op.getInputStream();
byte[] by = new byte[1024];
int len = 0;
StringBuffer sb = new StringBuffer();
while ((len = in.read(by)) != -1) {
sb.append(new String(by, 0, len));
}
Log.i("tag", sb.toString());
//添加依賴或架包
Gson gson = new Gson();
Demo demo = gson.fromJson(sb.toString(), Demo.class);
List<Demo.T1348649079062Bean> t = demo.getT1348649079062();
Message message = new Message();
message.obj = t;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private void initView() {
li = (ListView) findViewById(R.id.li);
}
}
//實體類
class Demo {
private List<T1348649079062Bean> T1348649079062;
public List<T1348649079062Bean> getT1348649079062() {
return T1348649079062;
}
public void setT1348649079062(List<T1348649079062Bean> T1348649079062) {
this.T1348649079062 = T1348649079062;
}
public static class T1348649079062Bean {
/**
* imgextra : [{"imgsrc":"http://cms-bucket.ws.126.net/2019/06/04/e541848f35944bd0918aceb9dd9119e3.jpeg"},{"imgsrc":"http://cms-bucket.ws.126.net/2019/06/04/d4099cce607844b3977c54d0be7bc4e0.jpeg"}]
* template : recommend
* skipID : 00DE0005|165797
* lmodify : 2019-06-04 10:34:05
* postid : PHOT51T5000500DE
* source : 網易綜合
* title : 巴薩釋出紅藍格子新球衣 梅西當模特
* mtime : 2019-06-04 10:34:05
* hasImg : 1
* topic_background : http://img2.cache.netease.com/m/newsapp/reading/cover1/C1348649048655.jpg
* digest :
* photosetID : 00DE0005|165797
* boardid : sports2_bbs
* alias : Sports
* hasAD : 1
* imgsrc : http://cms-bucket.ws.126.net/2019/06/04/fccc1e3d1ad44fc887cd0f1419fe3b81.jpeg
* ptime : 2019-06-04 10:16:59
* daynum : 18051
* hasHead : 1
* order : 1
* votecount : 1571
* hasCover : false
* docid : 9IG74V5H00963VRO_EGQOPCMMbjliudongchenupdateDoc
* tname : 體育
* priority : 252
* ename : tiyu
* replyCount : 1798
* picinfo : {"firstImage":"http://pic-bucket.ws.126.net/photo/0005/2019-06-04/EGQNKTB200DE0005NOS.jpg"}
* imgsum : 11
* hasIcon : true
* skipType : photoset
* category : 體育,國際足球
* cid : C1348649048655
* url_3w : http://sports.163.com/19/0604/17/EGRG023Q00058780.html
* url : http://3g.163.com/sports/19/0604/17/EGRG023Q00058780.html
* ltitle : 官宣!中國成2023年亞洲杯主辦國 15年前主場奪亞軍
* subtitle :
* specialID : S1554974040914
*/
private String template;
private String skipID;
private String lmodify;
private String postid;
private String source;
private String title;
private String mtime;
private int hasImg;
private String topic_background;
private String digest;
private String photosetID;
private String boardid;
private String alias;
private int hasAD;
private String imgsrc;
private String ptime;
private String daynum;
private int hasHead;
private int order;
private int votecount;
private boolean hasCover;
private String docid;
private String tname;
private int priority;
private String ename;
private int replyCount;
private PicinfoBean picinfo;
private int imgsum;
private boolean hasIcon;
private String skipType;
private String category;
private String cid;
private String url_3w;
private String url;
private String ltitle;
private String subtitle;
private String specialID;
private List<ImgextraBean> imgextra;
public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}
public String getSkipID() {
return skipID;
}
public void setSkipID(String skipID) {
this.skipID = skipID;
}
public String getLmodify() {
return lmodify;
}
public void setLmodify(String lmodify) {
this.lmodify = lmodify;
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMtime() {
return mtime;
}
public void setMtime(String mtime) {
this.mtime = mtime;
}
public int getHasImg() {
return hasImg;
}
public void setHasImg(int hasImg) {
this.hasImg = hasImg;
}
public String getTopic_background() {
return topic_background;
}
public void setTopic_background(String topic_background) {
this.topic_background = topic_background;
}
public String getDigest() {
return digest;
}
public void setDigest(String digest) {
this.digest = digest;
}
public String getPhotosetID() {
return photosetID;
}
public void setPhotosetID(String photosetID) {
this.photosetID = photosetID;
}
public String getBoardid() {
return boardid;
}
public void setBoardid(String boardid) {
this.boardid = boardid;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public int getHasAD() {
return hasAD;
}
public void setHasAD(int hasAD) {
this.hasAD = hasAD;
}
public String getImgsrc() {
return imgsrc;
}
public void setImgsrc(String imgsrc) {
this.imgsrc = imgsrc;
}
public String getPtime() {
return ptime;
}
public void setPtime(String ptime) {
this.ptime = ptime;
}
public String getDaynum() {
return daynum;
}
public void setDaynum(String daynum) {
this.daynum = daynum;
}
public int getHasHead() {
return hasHead;
}
public void setHasHead(int hasHead) {
this.hasHead = hasHead;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public int getVotecount() {
return votecount;
}
public void setVotecount(int votecount) {
this.votecount = votecount;
}
public boolean isHasCover() {
return hasCover;
}
public void setHasCover(boolean hasCover) {
this.hasCover = hasCover;
}
public String getDocid() {
return docid;
}
public void setDocid(String docid) {
this.docid = docid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getReplyCount() {
return replyCount;
}
public void setReplyCount(int replyCount) {
this.replyCount = replyCount;
}
public PicinfoBean getPicinfo() {
return picinfo;
}
public void setPicinfo(PicinfoBean picinfo) {
this.picinfo = picinfo;
}
public int getImgsum() {
return imgsum;
}
public void setImgsum(int imgsum) {
this.imgsum = imgsum;
}
public boolean isHasIcon() {
return hasIcon;
}
public void setHasIcon(boolean hasIcon) {
this.hasIcon = hasIcon;
}
public String getSkipType() {
return skipType;
}
public void setSkipType(String skipType) {
this.skipType = skipType;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getUrl_3w() {
return url_3w;
}
public void setUrl_3w(String url_3w) {
this.url_3w = url_3w;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getLtitle() {
return ltitle;
}
public void setLtitle(String ltitle) {
this.ltitle = ltitle;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getSpecialID() {
return specialID;
}
public void setSpecialID(String specialID) {
this.specialID = specialID;
}
public List<ImgextraBean> getImgextra() {
return imgextra;
}
public void setImgextra(List<ImgextraBean> imgextra) {
this.imgextra = imgextra;
}
public static class PicinfoBean {
/**
* firstImage : http://pic-bucket.ws.126.net/photo/0005/2019-06-04/EGQNKTB200DE0005NOS.jpg
*/
private String firstImage;
public String getFirstImage() {
return firstImage;
}
public void setFirstImage(String firstImage) {
this.firstImage = firstImage;
}
}
public static class ImgextraBean {
/**
* imgsrc : http://cms-bucket.ws.126.net/2019/06/04/e541848f35944bd0918aceb9dd9119e3.jpeg
*/
private String imgsrc;
public String getImgsrc() {
return imgsrc;
}
public void setImgsrc(String imgsrc) {
this.imgsrc = imgsrc;
}
}
}
}
//BaseAdapter擴充卡
class MyAdapter extends BaseAdapter {
private List<Demo.T1348649079062Bean> list;
private Context context;
public MyAdapter(List<Demo.T1348649079062Bean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHodle hodle = null;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.item,null);
hodle.category = convertView.findViewById(R.id.category);
hodle.title = convertView.findViewById(R.id.title);
convertView.setTag(hodle);
}else{
hodle = (ViewHodle) convertView.getTag();
}
Demo.T1348649079062Bean t1348649079062Bean = list.get(position);
hodle.title.setText(t1348649079062Bean.getTitle());
hodle.category.setText(t1348649079062Bean.getCategory());
return convertView;
}
class ViewHodle{
TextView title,category;
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@+id/li"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
//ListView布局
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="tilte" />
<TextView
android:id="@+id/category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="category" />