Android 使用 Volley获取网络图片通过Glide填充到Listview
导入依赖
Glide
implementation 'com.github.bumptech.glide:glide:4.14.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.14.2'
Volley
implementation 'com.android.volley:volley:1.2.1'
布局样式
MainActivity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
item.xml(子容器)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageview"
android:layout_width="120dp"
android:layout_height="100dp"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</LinearLayout>
后端代码
存储数据的实体类Good
public class Good {
private String name;
private String pic;
public Good(String name, String pic) {
this.name = name;
this.pic = pic;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
List View使用到的Adapter,这里使用BaseAdapter
public class Myadapter extends BaseAdapter {
List<Good> list;
Context context;
public Myadapter(Context context, List<Good> list) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
viewHolder = new ViewHolder();
view = View.inflate(context,R.layout.item,null);
viewHolder.imageView = view.findViewById(R.id.imageview);
viewHolder.textView = view.findViewById(R.id.textview);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag();
}
Good good = list.get(i);
viewHolder.textView.setText(good.getName());
Glide.with(context).load(good.getPic()).into(viewHolder.imageView);
return view;
}
class ViewHolder {
TextView textView;
ImageView imageView;
}
}
使用Volley获取网页图片
public void getimage(){
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);//请求队列
String url = "https://api-hmugo-web.itheima.net/api/public/v1/categories";//数据链接
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) { //JSON字符串处理
try {
JSONObject jsonObject = new JSONObject(response.toString());
JSONArray js = jsonObject.getJSONArray("message");
list = new ArrayList<Good>();
for(int i = 0;i<js.length();i++){
JSONObject child1 = (JSONObject) js.get(i);
JSONArray jsonArray = child1.getJSONArray("children");
for(int j = 0 ;j < jsonArray.length();j++){
JSONObject child2 = (JSONObject) jsonArray.get(j);
if(child2.has("children")){
JSONArray jsonArray2 = child2.getJSONArray("children");
for(int k = 0;k<jsonArray2.length();k++){
JSONObject jsonObject3 = jsonArray2.getJSONObject(k);
String cat_name = jsonObject3.getString("cat_name");
String cat_icon = jsonObject3.getString("cat_icon");
Log.e("result",cat_name+cat_icon);
Good good = new Good(cat_name,cat_icon);
list.add(good);
}
}
}
}
ListView listView = findViewById(R.id.listview);
Myadapter myadapter = new Myadapter(MainActivity.this,list);
listView.setAdapter(myadapter);
}catch (Exception e){
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
Oncreate函数
List<Good> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getimage();
}
MainActivity完整代码
public class MainActivity extends AppCompatActivity {
List<Good> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getimage();
}
public void getimage(){
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://api-hmugo-web.itheima.net/api/public/v1/categories";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response.toString());
JSONArray js = jsonObject.getJSONArray("message");
list = new ArrayList<Good>();
for(int i = 0;i<js.length();i++){
JSONObject child1 = (JSONObject) js.get(i);
JSONArray jsonArray = child1.getJSONArray("children");
for(int j = 0 ;j < jsonArray.length();j++){
JSONObject child2 = (JSONObject) jsonArray.get(j);
if(child2.has("children")){
JSONArray jsonArray2 = child2.getJSONArray("children");
for(int k = 0;k<jsonArray2.length();k++){
JSONObject jsonObject3 = jsonArray2.getJSONObject(k);
String cat_name = jsonObject3.getString("cat_name");
String cat_icon = jsonObject3.getString("cat_icon");
Log.e("result",cat_name+cat_icon);
Good good = new Good(cat_name,cat_icon);
list.add(good);
}
}
}
}
ListView listView = findViewById(R.id.listview);
Myadapter myadapter = new Myadapter(MainActivity.this,list);
listView.setAdapter(myadapter);
}catch (Exception e){
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
}
乌云后面依然是灿烂的晴天