毕加索用HTTP帖子加载图片 [英] Picasso load image with HTTP post

查看:183
本文介绍了毕加索用HTTP帖子加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API需要一些验证机制来验证每个HTTP请求。 API具有使用HTTP post方法加载图像的功能。 post请求体包含一个JSON对象,该对象从服务器端验证。

My API needs some verification mechanism for verifying every HTTP request. The API have the functionality to load a image using HTTP post method. The post request body have contain a JSON object which is verified from the server side.

为此我需要在http post请求体上包含这样的json。 / p>

For that i need to include a json like this on the http post request body.

{
    "session_id": "someId",
    "image_id": "some_id"
}

我怎样才能用毕加索做这个?

how can I do this with Picasso ?

推荐答案

我从Mr.Jackson Chengalai提供的提示中得到了解决方案。

I got the solution from the hint given by Mr.Jackson Chengalai.

创建一个Okhttp请求拦截器

Create a Okhttp request interceptor

private static class PicassoInterceptor implements Interceptor {

    @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("session_id", session_id);
        map.put("image", image);
        String requestJsonBody = new Gson().toJson(map);
        RequestBody body = RequestBody.create(JSON, requestStringBody);
        final Request original = chain.request();
        final Request.Builder requestBuilder = original.newBuilder()
                .url(url)
                .post(body);
        return chain.proceed(requestBuilder.build());
    }
}

创建一个Okhttp客户端添加此拦截器

Create a Okhttp client add this interceptor

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new PicassoInterceptor());

使用此okhttp客户端创建Dowloader

Create a Dowloader using this okhttp client

OkHttpDownloader = downloader = new OkHttpDownloader(okHttpClient)

Build Picasso使用此下载程序

Build Picasso using this downloader

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

这篇关于毕加索用HTTP帖子加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆