【问题标题】:How to retrieve image from a JSON Response from API using retrofit如何使用改造从 API 的 JSON 响应中检索图像
【发布时间】:2023-03-17 21:40:02
【问题描述】:

我收到一个 JSON 响应,其中显示了我的图像路径,但我不能直接使用改造从它的路径加载图像。

这是 JSON 输出示例:

{
"Emp_Photo": "/Images/2018_07_02_05_30_24.jpg",
}

我尝试获取 Emp_Photo 的值并将其存储到字符串中,并使用 picasso 将字符串加载到 imageview 中,但它不起作用。

这是我正在尝试完成的部分代码:

final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Fetching Data...");
        progressDialog.show();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiClient.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Fetchemployeedetailsinterface service = retrofit.create(Fetchemployeedetailsinterface.class);

//Result is our pojo class
        SharedPreferences settings = getActivity().getSharedPreferences("PREFS_NAME", 0);
       String emailtoken= settings.getString("email", "").toString();
        Call<ResponseData> call = service.Bind_Employee_Details_Based_On_Id(emailtoken);
        call.enqueue(new Callback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                //.getMessage is POJO method to listen for final json output
                List<MessageItem> listResponse =response.body().getMessage();
                String fname=listResponse.get(0).getEmpFirstName();
                String lname=listResponse.get(0).getEmpLastName();
                String email=listResponse.get(0).getEmpEmail();
                String address=listResponse.get(0).getEmpAddress();
                String joindt=listResponse.get(0).getJoiningDate();
                String imgaddress=listResponse.get(0).getEmpPhoto();

                Picasso.with(getActivity()).load(imgaddress).into(pick);
                ettvname.setText(fname+"-"+lname);
                etfname.setText(fname);
                etlname.setText(lname);
               etemail.setText(email);
               etaddress.setText(address);
               etjoindt.setText(joindt);
                progressDialog.dismiss();
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                progressDialog.dismiss();
                Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

【问题讨论】:

    标签: android json image retrofit picasso


    【解决方案1】:

    您的Emp_Photo 只是图像的路径。您应该将其转换为完整的 URL,然后使用 Picasso 加载它。

    String imageUrl = YOUR_ROOT_IMAGE + imgaddress
    Picasso.with(getActivity()).load(imageUrl).into(pick);
    

    其他建议:

    并且您应该应用一些约定以使您的代码更易于阅读。

    service.Bind_Employee_Details_Based_On_Id() -> service.bindEmployeeDetailsBasedOnId()
    Fetchemployeedetailsinterface -> FetchEmployeeDetailsInterface
    ...
    

    这里:

    List<MessageItem> listResponse =response.body().getMessage();
    String fname=listResponse.get(0).getEmpFirstName();
    

    您应该检查并确保 listResponse 至少有 1 项,以防止您的应用在运行时崩溃。

    【讨论】:

      【解决方案2】:

      正如您在描述中提到的那样,您将获得图像的唯一路径。但是,您没有获得图像的完整路径。因此,您应该需要从其中获取图像并轻松加载到图像视图中的图像填充路径。为此,您必须遵循以下方法:

      String image = base_URL + imgaddress;  //Here base URL means initial path of your image or server.
      Picasso.with(getActivity()).load(imgaddress).into(pick);
      

      尝试获取 base_URL 作为响应或其他方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-13
        • 1970-01-01
        • 2017-10-10
        • 2014-10-17
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 2020-03-12
        相关资源
        最近更新 更多