如何在 recyclerview 中使用 Retrofit 1.9 从另一个 api 设置图像

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

我正在使用 retrofit 1.9 从其他 api 中获取数据以显示在 recyclerview 中。到目前为止,我正在获取有关用户的所有必要信息。现在我还想在那个 recyclerview 上显示用户的图像。现在对于用户信息,我有一个 api。像“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 我正在尝试使用 'com.squareup.picasso:picasso:2.5.2' 添加图像。但是在 运行 程序之后,我在 recyclerview 上看不到任何东西。

这是我的用户模型 class。我想提一下 json 文件中没有图像文件。

用户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"
 },
 ...
]

我的模型class是

public class ColleagueModel {

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

我要给一部分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);
        }
    });

基础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{

    }

   }

部分细节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 服务

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

持有人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);

    }
}

如何使用 picasso 在 recyclerview 中添加图像。

登录猫

    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

将图片 Url 传递到详细页面

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

并获取图像

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