第一步:权限
<!--添加网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
第二步:ListView和里面的布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</RelativeLayout>
第三步:MainActivity
public class MainActivity extends AppCompatActivity {
public static final String TAG="MainActivity";
// https://blog.csdn.net/niu_yue_jiao/article/details/79221239
private ListView lv;
private List<FocusBean> list;
public String url = "http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt";
private URL murl;
private TextView mtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyAsyn().execute();
}
class MyAsyn extends AsyncTask<Integer,Integer,String>{
//耗时的操作
@Override
protected String doInBackground(Integer... integers) {
Log.i(TAG, "doInBackground: ");
String messages = "";
try {
URL murl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) murl.openConnection();
//设置请求发送
connection.setRequestMethod("GET");
//请求超时
connection.setConnectTimeout(5 * 1000);
connection.setReadTimeout(5 * 1000);
//数据流处理
if(connection.getResponseCode()==200){
//获取输入流
InputStream inputStream= connection.getInputStream();
//读取输入流
byte[] b=new byte[1024*512]; //定义一个byte数组读取输入流
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //定义缓存流来保存输入流的数据
int len=0;
while((len=inputStream.read(b))>-1){ //每次读的len>-1 说明是是有数据的
baos.write(b,0,len); //三个参数 输入流byte数组 读取起始位置 读取终止位置
}
messages = baos.toString();
inputStream.close();
connection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
return messages;
}
//当后台操作结束时,此方法将会被调用,计算结果将做为参数传递到此方法中,直接将结果显示到UI组件上
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s!=null){
Log.i(TAG, "onPostExecute: "+s);
Gson gson = new Gson();
FocusBean focusBean = gson.fromJson(s, FocusBean.class);
List<FocusBean.ResultBean.DataBean> resultBean;
resultBean = focusBean.getResult().getData();
if (resultBean != null) {
MyAdapte myAdapte = new MyAdapte(MainActivity.this, resultBean);
lv.setAdapter(myAdapte);
}
}
}
//更新进度
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i(TAG, "onProgressUpdate: ");
Toast.makeText(MainActivity.this,"加载中。。。"+values.toString(),Toast.LENGTH_LONG).show();
}
//执行后台任务前对UI做一些标记
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i(TAG, "onPreExecute: ");
lv = findViewById(R.id.lv);
}
}
}
第四步:Bean
public class FocusBean implements Serializable {
private String reason;
private ResultBean result;
private int error_code;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public static class ResultBean {
private String stat;
private List<DataBean> data;
public String getStat() {
return stat;
}
public void setStat(String stat) {
this.stat = stat;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String uniquekey;
private String title;
private String date;
private String category;
private String author_name;
private String url;
private String thumbnail_pic_s;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
}
}
}
第五步:ListView适配器
public class MyAdapte extends BaseAdapter {
private LayoutInflater mInflater;
Context context;
List<FocusBean.ResultBean.DataBean> list;
private TextView textView;
//构造方法
public MyAdapte(Context context, List<FocusBean.ResultBean.DataBean> resultBean) {
this.context=context;
this.list=resultBean;
mInflater = LayoutInflater.from(context);
}
//统计总条目数量
@Override
public int getCount() {
return list.size();
}
//当前显示的条目
@Override
public Object getItem(int position) {
return list.get(position);
}
//获取当前条目id
@Override
public long getItemId(int position) {
return position;
}
//加载视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//加载布局
convertView = mInflater.inflate(R.layout.item_listview, parent, false);
holder= new ViewHolder();
TextView textView1 = holder.textView1 = convertView.findViewById(R.id.textView1);
TextView textView2 = holder.textView2 = convertView.findViewById(R.id.textView2);
TextView textView3 = holder.textView3 = convertView.findViewById(R.id.textView3);
ImageView imageView = holder.imageView = convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else { //else里面说明,convertView已经被复用了,说明convertView中已经设置过tag了,即holder
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(list.get(position).getAuthor_name());
holder.textView2.setText(list.get(position).getTitle());
holder.textView3.setText(list.get(position).getDate());
//图片异步加载使用Imageloader
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.imageView);
return convertView;
}
private class ViewHolder {
TextView textView1,textView2,textView3;
ImageView imageView;
}
}
第六步:ImageLoader清单文件配置
android:name=".myapp.Application"
第七步:ImageLoader
public class Application extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
//初始化ImageLoader
// ImageLoaderConfiguration 配置Imageloader;
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(build);//全局初始化此配置
}
}