【问题标题】:How to set Image from another api using Retrofit 1.9 in recyclerview如何在 recyclerview 中使用 Retrofit 1.9 从另一个 api 设置图像
【发布时间】:2018-01-28 14:53:15
【问题描述】:

我正在使用改造 1.9 从 rest api 获取数据以显示在 recyclerview 中。到目前为止,我正在获取有关用户的所有必要信息。现在我还想在那个 recyclerview 上显示用户的图像。现在对于用户信息,我有一个 api。比如“https://sample.app.com/api/user”。在这个 api 中,我没有任何图像字段。要收集用户图像,我还有另一个 api,例如 "https://sample.app.com/api/profileimage/"+mail。我正在尝试使用 'com.squareup.picasso:picasso:2.5.2' 添加图像。但是运行程序后,我在 recyclerview 上看不到任何东西。

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

用户接口

[
 {
    "_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"
 },
 ...
]

我的模型类是

public class ColleagueModel {

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

我要给一部分adpater类

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

基本网址

 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{

    }

   }

详细活动的一部分

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

持有人等级

 @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

【问题讨论】:

  • 你能从这个网址获取图片吗Constants.HTTP.PHOTO_URL + currentColleague.mail,你在浏览器中查看吗?
  • @BhuvaneshBs 在网络浏览器上是的,我正在获取图像
  • https://.../api/profileimage/mailadess。但是我没有关于模型类的任何字段。因为图像 api 完全不同。在用户 api 中,我没有图像选项。如何将此图像与此用户信息结合起来
  • @BhuvaneshBs 我给了我的 Logcat
  • 您的实际 API 服务是什么?与这个问题无关的那个

标签: android json api retrofit picasso


【解决方案1】:

将图片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);

【讨论】:

  • 此代码不起作用。它表明我不能以这种方式申请
  • 我需要为图像创建一个模型类吗?或者我可以在 ColleagueModel 中添加这个图像属性吗?
  • 对于图像我没有任何 json 数据。只喜欢 http url 图片。我怎样才能以正确的方式添加这个inamge,以便将来我也可以在数据库中添加这个图像?
  • 欢迎您:)。如果它的工作那么请接受答案,以便其他人可以得到帮助
猜你喜欢
  • 1970-01-01
  • 2020-07-24
  • 2016-06-21
  • 2020-11-14
  • 2016-08-03
  • 2016-09-16
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
相关资源
最近更新 更多