请求链接 https://api.bilibili.com/x/web-interface/ranking
1.添加网络权限 依赖等
implementation 'com.squareup.okhttp3:okhttp:4.9.3' implementation 'com.google.code.gson:gson:2.8.9'
2.写请求类network
package com.example.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.security.PublicKey;
import java.util.function.BiFunction;
import javax.xml.transform.OutputKeys;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class NetWork {
private static OkHttpClient client=new OkHttpClient.Builder()
.build();
//get请求方式
public static void get(String url, Callback callback){
Request request=new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(callback);
}
//post请求方式
public static void post(String url,Callback callback,String token,String json){
RequestBody body=RequestBody.create(MediaType.get(""),json);
Request request=new Request.Builder()
.url(url)
.post(body)
.build();
client.newCall(request).enqueue(callback);
}
//网络图片下载
public static void getimg(String url, ImageView img) throws IOException {
img.setTag(url);
Request request=new Request.Builder()
.url(url)
.build();
Response response=client.newCall(request).execute();
Bitmap bitmap= BitmapFactory.decodeStream(response.body().byteStream());
//判断图片是否重复显示
img.post(new Runnable() {
public void run() {
if (url.equals(img.getTag())) {
img.setImageBitmap(bitmap);
}
}
});
}
}
3.使用listview列表接收接口数据并显示
<ListView
android:id="@+id/video_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
4.编写bean类和适配器adapter 还有adapter页面的子布局
package com.example.myapplication.Bean;
import java.io.Serializable;
public class VideoBean implements Serializable {
private String img;
private String name;
private int id;
private String time;
public VideoBean(String img, String name, int id, String time) {
this.img = img;
this.name = name;
this.id = id;
this.time = time;
}
public int getId() {
return id;
}
public String getImg() {
return img;
}
public String getName() {
return name;
}
public String getTime() {
return time;
}
}
子布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<ImageView
android:id="@+id/img"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_gravity="center"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_gravity="center"
android:layout_marginTop="5dp"/>
</LinearLayout>
</LinearLayout>
Listview适配器adapter的编写
package com.example.myapplication.Adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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 android.widget.Toast;
import androidx.constraintlayout.helper.widget.Layer;
import com.example.myapplication.Bean.VideoBean;
import com.example.myapplication.NetWork;
import com.example.myapplication.R;
import com.example.myapplication.ToastUtil;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class VideoAdapter extends BaseAdapter {
private List<VideoBean> list;
private Context context;
public VideoAdapter(List<VideoBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.adapter_video,parent,false);
}
VideoBean bean=list.get(position);
ImageView imageView=convertView.findViewById(R.id.img);
TextView name=convertView.findViewById(R.id.name);
TextView time=convertView.findViewById(R.id.time);
name.setText(bean.getName());
time.setText(bean.getTime());
//开启子线程去下载图片
new Thread(new Runnable() {
public void run() {
try {
String url=bean.getImg();
NetWork.getimg(url,imageView);
}catch (Exception e){
ToastUtil.show(context,"图片下载失败");
}
}
}).start();
return convertView;
}
}
5.最后数据请求并且填充到listview上面
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<VideoBean>video_data=new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.video_list);
NetWork.get("https://api.bilibili.com/x/web-interface/ranking", new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
ToastUtil.show(MainActivity.this,"网络异常");
}
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
String json=response.body().string();
Map<String,Object> map=new Gson().fromJson(json, HashMap.class);
Map<String,Object>data= (Map<String, Object>) map.get("data");
List<Map<String, Object>>list= (List<Map<String, Object>>) data.get("list");
runOnUiThread(new Runnable() {
public void run() {
for (Map<String,Object>list:list){
Integer id;
try {
id=Integer.valueOf(list.get("aid").toString());
}catch (Exception e){
id=0;
}
VideoBean bean=new VideoBean(list.get("pic").toString(),
list.get("title").toString(), id,
list.get("duration").toString());
video_data.add(bean);
}
listView.setAdapter(new VideoAdapter(video_data,MainActivity.this));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VideoBean bean=video_data.get(position);
ToastUtil.show(MainActivity.this,bean.getId()+"");
}
});
}
});
}
});
}
}
6.写一个ToastUtil工具类(判断是否需要在子线程发送)
package com.example.myapplication;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import androidx.annotation.NonNull;
import org.jetbrains.annotations.NotNull;
public class ToastUtil {
private static Handler handler=new Handler(Looper.getMainLooper());
public static void show(Context ctx,String txt){
if (Looper.myLooper()==Looper.getMainLooper()){
Toast.makeText(ctx, txt, Toast.LENGTH_SHORT).show();
}else {
handler.sendEmptyMessage(0);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, txt, Toast.LENGTH_SHORT).show();
}
});
}
}
}
7.开始运行(效果图)