gpt4 book ai didi

Android Retrofit如何从api获取图片

转载 作者:行者123 更新时间:2023-11-29 19:43:13 25 4
gpt4 key购买 nike

我有一个 api,我可以从中获取信息,我想获取图像(如果有的话),然后将其放入 ListView 中。我得到了数据,但不明白如何获取图像,你能帮我吗?接口(interface)链接:http://gdetut.com/api/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1普通 View 下的API:http://jsonprettyprint.com/json-pretty-printer.php

此应用程序显示服务站和汽车配件商店,需要从 api 上传图像,如果是,最好的方法是什么?

改造类:

public class Retrofit {

private static final String ENDPOINT = "http://gdetut.com/api";
private static ApiInterface apiInterface;

interface ApiInterface {
@GET("/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1")
void getPlaces(Callback<List<Places>> callback);


}

static {
init();
}

private static void init() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(ENDPOINT)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
apiInterface = restAdapter.create(ApiInterface.class);
}

public static void getPlaces (Callback<List<Places>> callback) {
apiInterface.getPlaces(callback);
}

}

Activity 中的 Ratrofit 成功:

 Retrofit.getPlaces(new Callback<List<Places>>() {
@Override
public void success(List<Places> places, Response response) {


listView.setAdapter(new MyAdapter(MainActivity.this, places));


}

适配器:

 class MyAdapter extends ArrayAdapter<Places> {

public MyAdapter(Context context, List<Places> objects) {
super(context, R.layout.list_item, objects);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_id);
holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_name_id);
holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_name_id);
holder.imageView = (ImageView) rowView.findViewById(R.id.image_id);
holder.rating = (TextView) rowView.findViewById(R.id.rating_id);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}

Places places = getItem(position);
holder.nameOfPlace.setText(places.getName());
holder.subcategory_name.setText(places.getSubcategory_name());
holder.geometryName.setText(places.getGeometry_name());
holder.imageView.setImageResource(places.getImage());
holder.imageView.setImageResource(R.drawable.restaurant48);
holder.rating.setText(places.getRating());


return rowView;
}

class ViewHolder {

public TextView nameOfPlace;
public TextView subcategory_name;
public TextView geometryName;
public TextView rating;
public ImageView imageView;
}
}

地点类:

public class Places implements Serializable {


String name;
String geometry_name;
String rating;
String subcategory_name;
int image;

public Places(String name, String geometry_name, String rating,String subcategory_name, int image) {
this.name = name;
this.geometry_name = geometry_name;
this.rating = rating;
this.subcategory_name = subcategory_name;
this.image = image;
}



public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public String getGeometry_name() {
return geometry_name;
}

public void setGeometry_name(String geometry_name) {
this.geometry_name = geometry_name;
}

public String getRating() {
return rating;
}

public void setRating(String rating) {
this.rating = rating;
}

public String getSubcategory_name() {
return subcategory_name;
}

public void setSubcategory_name(String subcategory_name) {
this.subcategory_name = subcategory_name;
}

public int getImage() {
return image;
}

public void setImage(int image) {
this.image = image;
}

最佳答案

按照建议,您应该使用 picasso (为了进一步引用改造与 picasso、okhttp 和其他几个库同步工作)。

这样做的原因是,即使您将使用改造下载图像,您仍然需要将返回的数据转换为位图,以及缓存图像(如果您自己加载的图像多于少数)应用程序可能会因为没有正确处理你的内存使用而崩溃,幸运的是我们 picasso 自动这样做了)。

使用picasso使用gradle编译库:

compile 'com.squareup.picasso:picasso:2.5.2'

然后你就可以在任何你想要的地方使用它了,如下一个例子所示:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

我建议您查看我添加的链接,因为有更多选项可以添加到此代码(添加 View 持有者、调整大小等...)

关于Android Retrofit如何从api获取图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38269788/

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