Is manually inserting data into a okhttp cache safe?

北城余情 提交于 2019-12-13 02:52:28

问题


I'm trying to preload a okhttp cache with data from a REST endpoint e.g., persons/1,2,3 that returns the same data as person/1 using an interceptor. It works but it uses cache().put$okhttp(resp).

Is it safe to use cache().put$okhttp(resp) to manipulate the okhttp cache like done below?

            String json = encoder.writeValueAsString(object);
            Request newRequest = new Request.Builder().url(url).build();
            ResponseBody body = ResponseBody
                    .create(MediaType.parse("application/json"), json);
            Response resp = new Response.Builder().body(body)
                    .request(newRequest).code(200).protocol(HTTP_1_1)
                    .header("content-type", "application/json")
                    .message("OK").body(body)
                    .networkResponse(new Response.Builder().code(200)
                            .request(newRequest).protocol(HTTP_1_1)
                            .header("content-type", "application/json")
                            .message("OK").build())
                    .build();
            Sink cacheSink = getOk().cache().put$okhttp(resp).body();
            cacheSink.write(resp.body().source().getBuffer(),
                    json.getBytes().length);
            cacheSink.close();

来源:https://stackoverflow.com/questions/56987491/is-manually-inserting-data-into-a-okhttp-cache-safe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!