MainActivity代码
public class MainActivity extends AppCompatActivity {
String urlString = “http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
String urlBitmap = “https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg”;
// new ArrayList<>
private ListView list_view;
ArrayList<Product.DataBean> dataBeanList = new ArrayList<Product.DataBean>();
private MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_view = findViewById(R.id.list_view);
mAdapter = new MAdapter();
list_view.setAdapter(mAdapter);
new MAsycTask().execute(urlString);
}
private class MAdapter extends BaseAdapter {
@Override
public int getCount() {
return dataBeanList.size();
}
@Override
public Object getItem(int position) {
return dataBeanList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view1 = View.inflate(MainActivity.this, R.layout.item02, null);
TextView tv_name = view1.findViewById(R.id.tv_name);
ImageView iv = view1.findViewById(R.id.iv);
tv_name.setText(dataBeanList.get(position).getNews_summary());
new MBitmapAsyncTask(iv).execute(dataBeanList.get(position).getPic_url());
return view1;
}
}
private class MAsycTask extends AsyncTask<String, Void, String> {
/*
子线程,请求网络
*/
@Override
protected String doInBackground(String... strings) {
String netjosn = NetUtil.getNewJson(strings[0]);
return netjosn;
}
/*
主线程,更新UI
*/
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Product product = gson.fromJson(s, Product.class);
List<Product.DataBean> data = product.getData();
dataBeanList.addAll(data);把一个集合中的数据,全部放到另一个集合里;
mAdapter.notifyDataSetChanged();
}
}
private class MBitmapAsyncTask extends AsyncTask<String, Void, Bitmap> {
ImageView iv;
public MBitmapAsyncTask(ImageView iv) {
this.iv = iv;
}
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap = NetUtil.getNetBitmap(strings[0]);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
iv.setImageBitmap(bitmap);
}
}
}
NetUtil代码
class NetUtil {
public static String getNewJson(String string) {
try {
URL url = new URL(string);
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder=new StringBuilder();
String str="";
while ((str=reader.readLine())!=null){
builder.append(str);
}
return builder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static Bitmap getNetBitmap(String string) {
try {
URL url = new URL(string);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setConnectTimeout(8000);
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = urlConnection.getInputStream();
//BitmapFactory: android提供好的加工图片的工具类,
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}