问题
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