首先建项目建布局文件
<uses-permission android:name="android.permission.INTERNET"/>
设置网络导入的资源文件
public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
网络权限
在Main方法的OnCreateView中找到listView控件并设置适配写入方法用asyncTask引入资源
new MMasyncTask().execute(urlString);
class MMasyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String netjson = NetUtil_11.getNetBitmap(strings[0]);
return netjson;
}
@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();
dataBeanArrayList.addAll(data);
//适配器刷新
mAdapter.notifyDataSetChanged();
}
}
设置listView适配器
public static String 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();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String temp = "";
while ((temp = reader.readLine()) != null){
builder.append(temp);
}
String string1 = builder.toString();
return string1;
}else{
Log.e(tag,""+responseCode);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//返回一个空的字符串
return "";
}
用一键生成创建Bean类
private class MAdapter extends BaseAdapter {
@Override
public int getCount() {
return dataBeanArrayList.size();
}
@Override
public Object getItem(int position) {
return dataBeanArrayList.get(position);
}-
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(getActivity(),R.layout.item_11,null);
TextView textView = view.findViewById(R.id.tv_11);
ImageView imageView = view.findViewById(R.id.iv_11);
textView.setText(dataBeanArrayList.get(position).getNews_summary());
new AsyncBitmapTask(imageView).execute(dataBeanArrayList.get(position).getPic_url());
return view;
}
}
设置图片的AsyncTask
private class AsyncBitmapTask extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
public AsyncBitmapTask(ImageView imageView) {
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(String... strings) {
return NetUtil_11.getJson(strings[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
}
}
根据个人喜好设置请求的东西
public static Bitmap getJson(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();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}