GsonRequest extends Request<T> { public <R> GsonRequest<T> setRequestData(R requestData, Gson gson, TypeAdapter adapter) { try { super.setRequestData(requestData, new GsonRequestConverter<>(gson, adapter)); } catch (IOException e) { throw new RuntimeException("Unable to convert " + requestData + " to NetworkRequest", e); } setNetworkRequest(NetworkRequest.Builder.from(getNetworkRequest()) .setHeader("Accept", "application/json") .build()); return this; } GsonRequest(String method, HttpUrl url, TypeAdapter<T> typeAdapter);  GsonRequest(String method, HttpUrl url, Class<T> tClass, Gson gson);  GsonRequest(String method, HttpUrl url, Class<T> tClass);  GsonRequest(String method, String url, TypeAdapter<T> typeAdapter);  GsonRequest(String method, String url, Class<T> tClass, Gson gson);  GsonRequest(String method, String url, Class<T> tClass); GsonRequest<T> setRequestData(R requestData, Gson gson, TypeAdapter adapter); GsonRequest<T> setRequestData(R requestData, Gson gson); GsonRequest<T> setRequestData(R requestData);  }
@Test public void anInterface() throws IOException, InterruptedException, ExecutionException { server.enqueue(new MockResponse().setBody("{\"name\":\"value\"}")); GsonRequest<AnInterface> request = new GsonRequest<>(Request.Method.POST, server.url("").toString(), AnInterface.class, gson); request.setRequestData(new AnImplementation("value"), gson, new AnInterfaceAdapter()); AnInterface body = queue.add(request).getFuture().get(); assertThat(body.getName()).isEqualTo("value"); RecordedRequest sRequest = server.takeRequest(); assertThat(sRequest.getBody().readUtf8()).isEqualTo("{\"name\":\"value\"}"); assertThat(sRequest.getHeader("Content-Type")).isEqualTo("application/json; charset=UTF-8"); assertThat(sRequest.getHeader("Accept")).isEqualTo("application/json"); }
@Test public void anImplementation() throws IOException, InterruptedException, ExecutionException { server.enqueue(new MockResponse().setBody("{\"theName\":\"value\"}")); GsonRequest<AnImplementation> request = new GsonRequest<AnImplementation>(Request.Method.POST, server.url("").toString(), AnImplementation.class) .setRequestData(new AnImplementation("value"), gson); AnImplementation body = queue.add(request) .getFuture().get(); assertThat(body.theName).isEqualTo("value"); RecordedRequest sRequest = server.takeRequest(); assertThat(sRequest.getBody().readUtf8()).isEqualTo("{\"theName\":\"value\"}"); assertThat(sRequest.getHeader("Content-Type")).isEqualTo("application/json; charset=UTF-8"); assertThat(sRequest.getHeader("Accept")).isEqualTo("application/json"); }
@Test public void serializeUsesConfiguration() throws IOException, InterruptedException, ExecutionException { server.enqueue(new MockResponse().setBody("{}")); GsonRequest<AnImplementation> request = new GsonRequest<AnImplementation>(Request.Method.POST, server.url("").toString(), AnImplementation.class) .setRequestData(new AnImplementation(null)); queue.add(request).getFuture().get(); RecordedRequest sRequest = server.takeRequest(); assertThat(sRequest.getBody().readUtf8()).isEqualTo("{}"); assertThat(sRequest.getHeader("Content-Type")).isEqualTo("application/json; charset=UTF-8"); assertThat(sRequest.getHeader("Accept")).isEqualTo("application/json"); }