如何使用改型从API的JSON响应中检索图像

时间:2018-12-06 02:27:16

标签: android json image retrofit picasso

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

这是JSON输出示例:

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

我尝试获取Emp_Photo的值并将其存储到字符串中,然后使用毕加索将其加载到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();
            }
        });

2 个答案:

答案 0 :(得分:1)

您的 (function () { var routes = {}, defaultRoute = 'home'; routes['home'] = { url: '#/', templateUrl: 'pages/home.php', title: 'This is the Homepage' }; routes['about'] = { url: '#/about', templateUrl: 'pages/about.php', title: 'This is the About page' }; routes['contact'] = { url: '#/contact', templateUrl: 'pages/contact.php', title: 'This is the Contact page' }; $.router .setData(routes) .setDefault(defaultRoute); $.when($.ready) .then(function() { $.router.run('.my-view','home'); }); }()); 只是图像的路径。您应该将其转换为完整的URL,然后将其与Picasso一起加载。

Emp_Photo

其他建议:

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

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

在这里:

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

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

答案 1 :(得分:0)

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

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。