package com.example.rk2;
import java.util.List;
public class PersonBean {
public String message;
public Data data;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
class Data{
public List<Lista> pc_feed_focus;
public List<Lista> getPc_feed_focus() {
return pc_feed_focus;
}
public void setPc_feed_focus(List<Lista> pc_feed_focus) {
this.pc_feed_focus = pc_feed_focus;
}
class Lista{
public String title;
public String display_url;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDisplay_url() {
return display_url;
}
public void setDisplay_url(String display_url) {
this.display_url = display_url;
}
}
}
}
mainactivity
package com.example.rk2;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv;
//private List<PersonBean.Data.Lista> list=new ArrayList<>();
private MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv =findViewById(R.id.lv);
mAdapter = new MAdapter(this);
lv.setAdapter(mAdapter);
new Thread(new Runnable() {
@Override
public void run() {
requestData();
}
}).start();
}
private Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//list.addAll((List<PersonBean.Data.Lista>) msg.obj);
mAdapter.setList((List<PersonBean.Data.Lista>) msg.obj);
//mAdapter.notifyDataSetChanged();
}
};
private String apiUrl = "https://www.toutiao.com/api/pc/focus/";
private void requestData() {
try {
URL url = new URL(apiUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
String result=readStream(urlConnection.getInputStream(),"utf-8");
PersonBean personBean = new Gson().fromJson(result, PersonBean.class);
Message message = mHandler.obtainMessage(0, personBean.getData().getPc_feed_focus());
mHandler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String readStream(InputStream inputStream, String ss) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, ss);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
}
MAdapter
package com.example.rk2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MAdapter extends BaseAdapter {
private Context context;
private List<PersonBean.Data.Lista> list;
public MAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<PersonBean.Data.Lista> list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public PersonBean.Data.Lista 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) {
ViewHolder viewHolder;
if (convertView==null){
// 加载哪个布局 父布局是哪个 true会直接把item加载到parent;false不会
convertView=LayoutInflater.from(context).inflate(R.layout.linear,parent,false);
viewHolder=new ViewHolder(convertView);
}else{
viewHolder= (ViewHolder) convertView.getTag();
}
// viewHolder.title.setText(list.get(position).getTitle());
viewHolder.bindData(getItem(position));
return convertView;
}
class ViewHolder {
TextView title;
TextView date;
ImageView img;
public ViewHolder(View itemView){
title = itemView.findViewById(R.id.title);
date = itemView.findViewById(R.id.date);
img = itemView.findViewById(R.id.img);
itemView.setTag(this);
}
public void bindData(PersonBean.Data.Lista item) {
title.setText(item.getTitle());
}
}
}