Glide POST请求图片

时间:2017-06-26 14:56:39

标签: android android-recyclerview android-glide

我尝试使用POST(json body)从服务器下载图像。为此,我创建了okhttp拦截器:

    private static class PostThumbnailRequestInterceptor implements Interceptor {
    private static final String DEVICE_GUID    = "device_guid";
    private static final String RESOURCE_GUIDS = "resource_guids";
    private static final String UTC_TIMESTAMP  = "utc_timestamp";

    private String mChannelGuid;
    private String mResourceGuid;

    public PostThumbnailRequestInterceptor(String channelGuid, String resourceGuid) {
        mChannelGuid = channelGuid;
        mResourceGuid = resourceGuid;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {

        final MediaType JSON
                = MediaType.parse("application/json; charset=utf-8");
        JSONArray resources = new JSONArray();
        resources.put(mResourceGuid);
        JSONObject requestedThumbnail = new JSONObject();
        JSONObject payload = new JSONObject();
        try {
            requestedThumbnail.put(UTC_TIMESTAMP, System.currentTimeMillis() * 1000);
            requestedThumbnail.put(DEVICE_GUID, mChannelGuid);
            requestedThumbnail.put(RESOURCE_GUIDS, resources);
            payload.put("thumbnails", new JSONArray() {{put(requestedThumbnail);}});
        } catch (JSONException e) {
            throw new IOException("Failed to create payload");
        }

        RequestBody body = RequestBody.create(JSON, payload.toString());
        final Request original = chain.request();
        final Request.Builder requestBuilder = original.newBuilder()
                .url(original.url())
                .post(body);
        //return chain.proceed(requestBuilder.build());
        Response response = chain.proceed(requestBuilder.build());
        try {
            MediaType contentType = MediaType.parse("data:image/jpeg;base64");// response.body().contentType();
            JSONObject object = new JSONObject(response.body().string());
            String base64String = object.optJSONArray("thumbnails").getJSONObject(0).optString("content");
            base64String  = base64String.replace("data:image/jpeg;base64,", "");
            byte[] rawImage = Base64.decode(base64String , Base64.DEFAULT);
            ResponseBody realResponseBody = ResponseBody.create(contentType, rawImage);
            response = response.newBuilder().body(realResponseBody).build();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return response;
    }
}

并像这样使用

        OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new PostThumbnailRequestInterceptor(channel.id.getServerId(), channel.id.getChannelId()))
            .build();

    GlideApp.get(getContext())
            .getRegistry().replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(mOkHttpClient));

每次只能使用1个请求,但是如果我将它与recycleler视图绑定,则使用动态拦截器替换Glide中的类会产生很多错误(无法加载资源)。

我是否正确地要求发帖或者它只有一种方式 - 首先请求照常,然后将解码后的字节传递给Glide?

0 个答案:

没有答案