1

i have an api, where i get info and i want to get images, if they have too, and put it in ListView. Data i was get, but dont understand how to get images, can yoy help me? Api link: http://gdetut.com/api/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1 Api in normal view: http://jsonprettyprint.com/json-pretty-printer.php

This application shows the service stations and auto parts stores , it is necessary to upload images from api , if they are, the best way to do this ?

Retrofit class:

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 success in Activity:

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


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


        }

Adapter:

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

Places class:

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

}

Azarnoy
  • 167
  • 1
  • 1
  • 14
  • Seems a dup of [Retrofit API to retrieve a png image](http://stackoverflow.com/questions/25462523/retrofit-api-to-retrieve-a-png-image). – Sufian Jul 09 '16 at 03:50

5 Answers5

2

As suggested you should use picasso (for further reference retrofit works in sync with picasso, okhttp and few more libraries).

the reason for that is that even if you would download the image using retrofit you would still need to convert the data that will return into a bitmap, as well as caching images (if you will load more then few images by yourself your app will probably crash due to the lack of handling your memory usage properly, lucky for us picasso doing that automatically).

to use picasso compile the library using gradle:

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

then you can just use it wherever you want as mentioned in the next example:

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

I would suggest you though to go over the link I added since there are a lot more options to add to this code (adding view holder, resizing etc...)

Roee
  • 1,155
  • 3
  • 15
  • 24
  • 1
    i undesrstand it, but how can i get url of image from that json? – Azarnoy Jul 09 '16 at 06:42
  • Thanks. Recently I have plugged Refrofit and been choosing what an image library to use: Fresco, Glide or Picasso. – CoolMind Aug 05 '16 at 10:43
  • Here is a simple example of Picasso + Retrofit (though, an old version): http://themakeinfo.com/2015/04/android-retrofit-images-tutorial/ – CoolMind Aug 05 '16 at 11:22
1

As I see you can't receive image because you try to get from server field with image resource, but in json you don't have this field. Some image links can be in images json field. Btw, you can't use Retrofit for loading images(bitmap) with this query, right now you will can receive link and then load image in other way. So you can use some other library (for example Glide or Picassa).

Scott Key
  • 119
  • 5
  • How to use Picasso for this? – Azarnoy Jul 08 '16 at 17:05
  • After you receive String with image url just: `Picasso.with(context) .load(url) .into(holder.imageView)` where `url` is `String` which contains real URL (which you can open in desktop browser too) Btw you need add Picasso library in your project as gradle dependency, try to read more – http://square.github.io/picasso/ – Scott Key Jul 08 '16 at 17:29
  • tell me pls how i can raceive String with url? – Azarnoy Jul 08 '16 at 17:36
0

places.getImage() method will return image url. And you cant pass url to age view you need to decode stream.

Or u can use picasso library to easily set url to ageview

Learn Pain Less
  • 2,274
  • 1
  • 17
  • 24
0

You are trying to set the images as an image Resource:

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

But the API delivers the Url of the image. You should change the Datatype of image within Places to String and request the image with a image loading library. My personal recommendation would be Picasso.

dipdipdip
  • 2,326
  • 1
  • 21
  • 31
0

You're setting images as an image Resource.

Since the response gives you URL of the image, you need to use decoder to set the image to your imageView.

you can either use glide or picasso.

Add picasso dependency in your app level gradle file

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

and use this code..

// capture image into a string variable
String imgURL = places.getImage();
Picasso.with(context).load(imgURL).into(holder.imageView);
Suhas
  • 159
  • 1
  • 11
  • I'm sure there are other libraries you could use other than Glide or Picasso. –  Mar 22 '18 at 12:52