使用Picasso设置下载的图像(使用拦截器)

时间:2016-03-04 12:22:19

标签: android lazy-loading picasso

this回答之后,我试图使用Picasso在ListView中延迟加载图像。这是我的代码:

public class PicassoInterceptor implements Interceptor {

private String param1;
private String param2;
private String param3;
private int param4;
private String param5;

public PicassoInterceptor(String param1, String param2, String param3, int param4, String param5) {
    this.param1 = param1;
    this.param2 = param2;
    this.param3 = param3;
    this.param4 = param4;
    this.param5 = param5;
}

@Override
public Response intercept(Chain chain) throws IOException {
    final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    Map<String, String> map = new HashMap<String, String>();
    map.put("param1", param1);
    map.put("param2", param2);
    map.put("param3", param3);
    map.put("param4", String.valueOf(param4));
    String requestJsonBody = new Gson().toJson(map);
    RequestBody body = RequestBody.create(JSON, requestJsonBody);
    final Request original = chain.request();
    final Request.Builder requestBuilder = original.newBuilder()
            .url(param5)
            .post(body);
    return chain.proceed(requestBuilder.build());
}
}

适配器,在getView方法中:

OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(new PicassoInterceptor(param1, param2,param3,param4, param5));

    OkHttpDownloader downloader = new OkHttpDownloader(okHttpClient);
    Picasso picasso = new Picasso.Builder(context).downloader(downloader).build();

现在,如果我已正确理解,则会下载图像。但是,如何在图像视图中设置它?

编辑:

图像在BASE64中接收。

2 个答案:

答案 0 :(得分:1)

使用:

picasso.load(param5).fit().into(imageView);

你给load()函数添加哪个url并不重要 - 因为你在拦截器中覆盖了它。但我仍然给出一个合法的 - 以避免问题。

或者您可以通过以下方式编辑拦截器:Picasso load image with HTTP post

答案 1 :(得分:0)

只需添加Picasso picasso = new Picasso.Builder(context).into(imageView).downloader(downloader).build(); 方法并传递ImageView:

@POST
@Path("/notify")
@Consumes("*/*")
public Response notifyUser(@QueryParam("notification") String notification, @QueryParam("applicationName") String applicationName) {
    System.out.println("Notification: " + notification);
    System.out.println("Application: " + applicationName);
    return Response.status(200).entity(notification + " from " + applicationName).build();
}