Search code examples
androidjsonapiretrofitpicasso

How to set Image from another api using Retrofit 1.9 in recyclerview


I am using retrofit 1.9 to fetch data from rest api to show in recyclerview. So far I am geting all necessary information about user. Now I also want to show the image of users on that recyclerview. Now for user information I have one api. like "https://sample.app.com/api/user". In this api I do not have any image field. to collect user image I have another api like "https://sample.app.com/api/profileimage/"+mail.I am trying to add image by using 'com.squareup.picasso:picasso:2.5.2' . But after running the program , I cannot see any thing on recyclerview.

Here is my model class for User. I would like mention no impage filed is there in json file.

user api

[
 {
    "_id": "588797978",
    "dn": "CNdfgdfgdfg",
    "sn": "Das",
    "title": "title",
    "givenName": "Tom",
    "whenChanged": "20170623093215.0Z",
    "company": "Company limited",
    "name": "Soutrik Das",
    "mail": "mail@com.de",
    "mobile": "+123456",
    "updated_at": "2016-12-09T13:37:55.721Z"
 },
 ...
]

My Model class is

public class ColleagueModel {

public String _id,
        dn,
        givenName,
        whenChanged,
        name,
        mail,
        updatedAt,
        sn,
        title,
        department,
        company,
        mobile;
..............
}

I ma giving a portion of adpater class

@Override
public void onBindViewHolder(ColleagueHolder holder, int position) {

    final ColleagueModel currentColleague = mColleague.get(position);
    Picasso.with(holder.itemView.getContext());

    holder.colleagueName.setText(currentColleague.name);
    holder.companyName.setText(currentColleague.company);
    holder.jobTitle.setText(currentColleague.title);

    Picasso.with(holder.itemView.getContext()).load( Constants.HTTP.PHOTO_URL + currentColleague.mail).into(holder.colleaguePicture);

    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent i=new Intent(context,DetailMyColleague.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra(/* here I want to send image*/ )
            i.putExtra("name",currentColleague.name);
            i.putExtra("title",currentColleague.title);
            i.putExtra("company",currentColleague.company);
            i.putExtra("mobile",currentColleague.mobile);
            i.putExtra("mail",currentColleague.mail);
            i.putExtra("department",currentColleague.department);
            context.startActivity(i);
        }
    });

Base URL

 public class Constants {

    public static final class HTTP {
        public static final String BASE_URL = "https://sample.app.com";
        public static final String PHOTO_URL = BASE_URL + "/api/profileimage/";
    }
      public static final class DATABASE{

    }

   }

a portion of Detail activity

 Intent intent = getIntent();

    //RECEIVE DATA
    name = intent.getExtras().getString("name");
    int profileImage = intent.getIntExtra("image", 0);
    String title = intent.getExtras().getString("title");
    String company = intent.getExtras().getString("company");
    mobile = intent.getExtras().getString("mobile");
    String mail = intent.getExtras().getString("mail");
    String dept = intent.getExtras().getString("department");

    //BIND DATA
    _profilePic.setImageResource(profileImage);
    _profileName.setText(name);
    _colleagueName.setText(name);
    _mobile.setText(mobile);
    _mobile.setOnClickListener(this);
    _email.setText(mail);
    _email.setOnClickListener(this);
    _company.setText(company);
    _department.setText(dept);
    _jobRole.setText(title);

Api service

 public interface ColleagueApiService {
    @GET("/api/users")
    void getColleague(Callback<String> flowers);
}

Holder Class

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

public class ColleagueHolder extends RecyclerView.ViewHolder{

    public CardView cardView;
    public ImageView colleaguePicture;
    public TextView  colleagueName;
    public TextView  companyName;
    public TextView  jobTitle;

    public ColleagueHolder(View itemView) {
        super(itemView);
        colleaguePicture= itemView.findViewById(R.id.colleague_picture);
        colleagueName= itemView.findViewById(R.id.colleague_name);
        companyName= itemView.findViewById(R.id.company_name);
        jobTitle= itemView.findViewById(R.id.job_title);
        cardView= itemView.findViewById(R.id.cardview_user);

    }
}

How can I add image in recyclerview by using picasso.

Log Cat

    08-20 11:57:03.798 1581-2750/? I/ActivityManager: START u0 {cmp=demo.app.com.bluapp_client_and/.activity.myColleague.MyColleaguesPage} from uid 10164 on display 0
08-20 11:57:03.869 29300-29304/demo.app.com.bluapp_client_and I/art: Do partial code cache collection, code=54KB, data=55KB
08-20 11:57:03.871 29300-29304/demo.app.com.bluapp_client_and I/art: After code cache collection, code=45KB, data=50KB
08-20 11:57:03.871 29300-29304/demo.app.com.bluapp_client_and I/art: Increasing code cache capacity to 256KB
08-20 11:57:03.882 29300-29332/demo.app.com.bluapp_client_and D/EGL_emulation: eglMakeCurrent: 0xa4c164a0: ver 2 0 (tinfo 0xa49536c0)
08-20 11:57:03.970 29300-29300/demo.app.com.bluapp_client_and D/Controller: Error :: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
08-20 11:57:03.970 1581-1601/? I/ActivityManager: Displayed demo.app.com.bluapp_client_and/.activity.myColleague.MyColleaguesPage: +168ms
08-20 11:57:04.355 1581-1797/? I/WindowManager: Destroying surface Surface(name=demo.app.com.bluapp_client_and/demo.app.com.bluapp_client_and.activity.main.MainOptionPage) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.destroyOrSaveSurface:2073 com.android.server.wm.AppWindowToken.destroySurfaces:363 com.android.server.wm.AppWindowToken.notifyAppStopped:389 com.android.server.wm

Solution

  • Pass the image Url to detail Page

     holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                Intent i=new Intent(context,DetailMyColleague.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("IMAGE_URL",Constants.HTTP.PHOTO_URL + currentColleague.mail);
                i.putExtra("name",currentColleague.name);
                i.putExtra("title",currentColleague.title);
                i.putExtra("company",currentColleague.company);
                i.putExtra("mobile",currentColleague.mobile);
                i.putExtra("mail",currentColleague.mail);
                i.putExtra("department",currentColleague.department);
                context.startActivity(i);
            }
        });
    

    And get the image

        Intent intent = getIntent();
    
        //RECEIVE DATA
        name = intent.getExtras().getString("name");
        String profileImage = intent.getExtras().getString("IMAGE_URL");
        String title = intent.getExtras().getString("title");
        String company = intent.getExtras().getString("company");
        mobile = intent.getExtras().getString("mobile");
        String mail = intent.getExtras().getString("mail");
        String dept = intent.getExtras().getString("department");
    
        //BIND DATA
         Picasso.with(this).load(profileImage).into(_profilePic);
    
    //    _profilePic.setImageResource(profileImage);
        _profileName.setText(name);
        _colleagueName.setText(name);
        _mobile.setText(mobile);
        _mobile.setOnClickListener(this);
        _email.setText(mail);
        _email.setOnClickListener(this);
        _company.setText(company);
        _department.setText(dept);
        _jobRole.setText(title);