Picasso load image with HTTP post

前端 未结 1 1755
野的像风
野的像风 2020-12-21 05:18

My API having some verification mechanism for every HTTP request. One of the end-point have the functionality to load a image using HTTP post method. The post request body w

1条回答
  •  暖寄归人
    2020-12-21 05:24

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

    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 map = new HashMap();
            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());
        }
    }
    

    Create a Okhttp client add this interceptor

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

    Create a Dowloader using this okhttp client

    OkHttpDownloader = downloader = new OkHttpDownloader(okHttpClient)
    

    Build Picasso using this downloader

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

    0 讨论(0)
提交回复
热议问题