测试1 配置适配器
package com.example.app2;
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 com.bumptech.glide.Glide;
import java.util.List;
class MyAdapter extends BaseAdapter {
private List<NewsBean.TopStoriesBean> lists;
private Context context;
public MyAdapter(List<NewsBean.TopStoriesBean> lists, Context context) {
this.lists = lists;
this.context = context;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.layout_items,null);
holder.textView_title = convertView.findViewById(R.id.text_item);
holder.imageView_pic = convertView.findViewById(R.id.image_item);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView_title.setText(lists.get(position).getTitle());
if(lists.get(position).getImage().isEmpty() || lists.get(position).getImage() == null){
holder.imageView_pic.setImageResource(R.mipmap.ic_launcher);
}else {
Glide.with(context).load(lists.get(position).getImage()).into(holder.imageView_pic);
}
return convertView;
}
private class ViewHolder {
private TextView textView_title;
private ImageView imageView_pic;
}
}
测试2 创建异步任务
package com.example.app2;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public class MyAsyncTask extends AsyncTask<String,Void, List<NewsBean.TopStoriesBean>> {
private static final String TAG = "MyAsyncTask";
private List<NewsBean.TopStoriesBean> lists;
private Context context;
private MyAdapter adapter;
private ProgressBar progressBar ;
public MyAsyncTask(List<NewsBean.TopStoriesBean> lists, Context context, MyAdapter adapter, ProgressBar progressBar) {
this.lists = lists;
this.context = context;
this.adapter = adapter;
this.progressBar = progressBar;
}
@Override
protected List<NewsBean.TopStoriesBean> doInBackground(String... strings) {
String json = Http_GetJdonFromNet.getJsonFromUrl(strings[0]);
try {
JSONObject jsonObject = new JSONObject(json);
List<NewsBean.TopStoriesBean> top_stories1 = JSON.parseObject(json, NewsBean.class).getTop_stories();
JSONArray top_stories = jsonObject.getJSONArray("top_stories");
Log.i(TAG, "doInBackground: "+top_stories1);
if(top_stories1 == null){
Log.i("TAG", "doInBackground:数据获取失败");
}else {
return top_stories1;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<NewsBean.TopStoriesBean> topStoriesBeans) {
super.onPostExecute(topStoriesBeans);
progressBar.setVisibility(View.VISIBLE);
if(topStoriesBeans == null){
Toast.makeText(context, "json数据加载失败", Toast.LENGTH_SHORT).show();
}else {
lists.addAll(topStoriesBeans);
adapter.notifyDataSetChanged();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.setVisibility(View.GONE);
}
}
开启网络操作
package com.example.app2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Http_GetJdonFromNet {
public static String getJsonFromUrl(String path){
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(3000);
connection.setConnectTimeout(3000);
connection.connect();
if(connection.getResponseCode() == 200){
is = connection.getInputStream();
baos = new ByteArrayOutputStream();
int len = 0;
byte[] bys = new byte[1024];
while ((len = is.read(bys)) != -1){
baos.write(bys,0,len);
baos.flush();
}
String json = baos.toString();
return json;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if( is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if( baos != null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}