【问题标题】:Android Retrofit how to get images from apiAndroid Retrofit如何从api获取图片
【发布时间】:2018-03-26 13:58:17
【问题描述】:

我有一个 api,我可以在其中获取信息,并且我想获取图像,如果它们也有,并将其放入 ListView。我得到了数据,但不明白如何获取图像,你能帮我吗? api链接:http://gdetut.com/api/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1 普通视图下的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);
 }

}

Ratrofit 在 Activity 中的成功:

 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;
}

}

【问题讨论】:

标签: android retrofit


【解决方案1】:

按照建议,您应该使用picasso(进一步参考改造工程与毕加索、okhttp 和其他几个库同步)。

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

使用 picasso 使用 gradle 编译库:

编译'com.squareup.picasso:picasso:2.5.2'

然后你可以在任何你想要的地方使用它,如下一个例子中提到的:

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

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

【讨论】:

  • 我不理解它,但是我怎样才能从那个 json 中获取图像的 url?
  • 谢谢。最近我插入了 Refrofit 并一直在选择要使用的图像库:Fresco、Glide 或 Picasso。
  • 这里是 Picasso + Retrofit 的简单示例(虽然是旧版本):themakeinfo.com/2015/04/android-retrofit-images-tutorial
【解决方案2】:

正如我所见,您无法接收图像,因为您尝试使用 image 资源从服务器字段获取,但在 json 中您没有此字段。一些图片链接可以在images json 字段中。 顺便说一句,您不能使用 Retrofit 通过此查询加载图像(位图),现在您可以接收链接,然后以其他方式加载图像。所以你可以使用其他一些库(例如 Glide 或 Picassa)。

【讨论】:

  • 如何使用毕加索?
  • 在您收到带有图片网址的字符串后:Picasso.with(context) .load(url) .into(holder.imageView) 其中urlString,其中包含真实的网址(您也可以在桌面浏览器中打开)顺便说一句,您需要在您的项目作为 gradle 依赖,尝试阅读更多 - square.github.io/picasso
  • 请告诉我如何使用 url 竞速字符串?
【解决方案3】:

places.getImage() 方法将返回图像 url。而且您不能将 url 传递给需要解码流的年龄视图。

或者你可以使用毕加索库轻松设置 url 到 ageview

【讨论】:

  • 你的模型类是错误的。图像不是整数。它是一个数组。
  • 毕加索怎么做?
  • 首先解析图片的url。您的模型类不准确。
  • 如何解析图片的ulr?我必须写 int[] image 或 String[] image?
  • 有多张图片,你要哪一张??
【解决方案4】:

您正在尝试将图像设置为图像资源:

holder.imageView.setImageResource(places.getImage());

但 API 会提供图片的 Url。您应该将Places 中的image 的Datatype 更改为String,并使用图像加载库请求图像。我个人的建议是Picasso

【讨论】:

  • 毕加索怎么做?
  • 如何获取字符串中的图片?
【解决方案5】:

您将图像设置为图像资源。

由于响应为您提供了图像的 URL,您需要使用解码器将图像设置为您的 imageView。

您可以使用滑翔或毕加索。

在您的应用级 gradle 文件中添加 picasso 依赖项

compile 'com.squareup.picasso:picasso:2.71'

并使用此代码..

// capture image into a string variable
String imgURL = places.getImage();
Picasso.with(context).load(imgURL).into(holder.imageView);

【讨论】:

  • 我确信除了 Glide 或 Picasso 之外,您还可以使用其他库。
猜你喜欢
  • 2022-01-23
  • 2020-05-27
  • 2018-10-21
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多