gpt4 book ai didi

java - 如何缓存从 API 提取的数据中的图像和文本

转载 作者:行者123 更新时间:2023-12-02 13:16:16 24 4
gpt4 key购买 nike

我的第一个应用程序需要一些帮助。我制作了一个旅行指南应用程序,它使用改造 API 从 JSON 中提取信息和图像链接。

我的应用程序在线运行良好,可以提取所有数据,但现在我希望用户在缓存后能够离线查看所有信息。

客户端API

public static final String BASE_URL = "https://quarkbackend.com";
public static Retrofit retrofit = null;

public static Retrofit getClient(){
if (retrofit==null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}

服务API

public interface Service {
@GET("/getfile/beyond-infinity-gd/homepage")
Call<AlbumsResponse> getHomepage();

@GET("/getfile/beyond-infinity-gd/tours")
Call<AlbumsResponse> getTours();

...

适配器

public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder> {

private Context mContext;
private List<Album> albumList;

public AlbumsAdapter(Context mContext, List<Album> albumList) {
this.mContext = mContext;
this.albumList = albumList;
}

@Override
public AlbumsAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.album_card, viewGroup, false);

return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(final AlbumsAdapter.MyViewHolder viewHolder, int i) {
viewHolder.title.setText(albumList.get(i).getTitle());
viewHolder.count.setText(albumList.get(i).getSubtitle());

//load album cover using picasso
Picasso.with(mContext)
.load(albumList.get(i).getThumbnail())
.placeholder(R.drawable.load)
.into(viewHolder.thumbnail);


}

@Override
public int getItemCount() {
return albumList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count;
public ImageView thumbnail;

public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
count = (TextView) view.findViewById(R.id.count);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);

//on item click
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION){
Album clickedDataItem = albumList.get(pos);
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("title", albumList.get(pos).getTitle());
intent.putExtra("subtitle", albumList.get(pos).getSubtitle());
intent.putExtra("body", albumList.get(pos).getBody());
intent.putExtra("thumbnail", albumList.get(pos).getThumbnail());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
});
}
}

最佳答案

要缓存文本数据,您可以使用 SQLite。检查this教程和缓存图像,你应该看看 Glide/Picasso 库。他们可以将图像存储在内存/磁盘中。

关于java - 如何缓存从 API 提取的数据中的图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43762218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com