Search in sources :

Example 31 with Response

use of okhttp3.Response in project MVCHelper by LuckyJayce.

the class HttpMethod method executeSync.

/**
     * 执行同步请求
     *
     * @return 返回请求回来的 Response
     * @throws Exception
     */
public final Response executeSync() throws Exception {
    Request request = buildRequest();
    call = client.newCall(request);
    return call.execute();
}
Also used : Request(okhttp3.Request)

Example 32 with Response

use of okhttp3.Response in project MVCHelper by LuckyJayce.

the class BooksOkHttp_SyncDataSource method loadBooks.

private List<Book> loadBooks(final int page) throws Exception {
    GetMethod method = new GetMethod("https://www.baidu.com");
    method.addHeader("a", "aaaaa");
    method.addParam("api_key", "75ee6c644cad38dc8e53d3598c8e6b6c");
    List<Book> data = method.executeSync(new ResponseParser<List<Book>>() {

        @Override
        public List<Book> parse(Response response) throws Exception {
            Thread.sleep(1000);
            List<Book> books = new ArrayList<Book>();
            for (int i = 0; i < 15; i++) {
                books.add(new Book("page" + page + "  Java编程思想 " + i, 108.00d));
            }
            mPage = page;
            return books;
        }
    });
    return data;
}
Also used : Response(okhttp3.Response) Book(com.shizhefei.test.models.enties.Book) GetMethod(com.shizhefei.mvc.http.okhttp.GetMethod) List(java.util.List) ArrayList(java.util.ArrayList)

Example 33 with Response

use of okhttp3.Response in project feign by OpenFeign.

the class BaseApiTest method resolvesBodyParameter.

@Test
public void resolvesBodyParameter() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("foo"));
    String baseUrl = server.url("/default").toString();
    Feign.builder().encoder(new Encoder() {

        @Override
        public void encode(Object object, Type bodyType, RequestTemplate template) {
            assertThat(bodyType).isEqualTo(new TypeToken<Keys<String>>() {
            }.getType());
        }
    }).decoder(new Decoder() {

        @Override
        public Object decode(Response response, Type type) {
            assertThat(type).isEqualTo(new TypeToken<Entities<String, Long>>() {
            }.getType());
            return null;
        }
    }).target(MyApi.class, baseUrl).getAll(new Keys<String>());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Decoder(feign.codec.Decoder) MockResponse(okhttp3.mockwebserver.MockResponse) Type(java.lang.reflect.Type) Encoder(feign.codec.Encoder) TypeToken(com.google.gson.reflect.TypeToken) Test(org.junit.Test)

Example 34 with Response

use of okhttp3.Response in project feign by OpenFeign.

the class BaseApiTest method resolvesParameterizedResult.

@Test
public void resolvesParameterizedResult() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("foo"));
    String baseUrl = server.url("/default").toString();
    Feign.builder().decoder(new Decoder() {

        @Override
        public Object decode(Response response, Type type) {
            assertThat(type).isEqualTo(new TypeToken<Entity<String, Long>>() {
            }.getType());
            return null;
        }
    }).target(MyApi.class, baseUrl).get("foo");
    assertThat(server.takeRequest()).hasPath("/default/api/foo");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Type(java.lang.reflect.Type) Decoder(feign.codec.Decoder) Test(org.junit.Test)

Example 35 with Response

use of okhttp3.Response in project feign by OpenFeign.

the class AbstractClientTest method testContentTypeWithoutCharset.

@Test
public void testContentTypeWithoutCharset() throws Exception {
    server.enqueue(new MockResponse().setBody("AAAAAAAA"));
    TestInterface api = newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
    Response response = api.postWithContentType("foo", "text/plain");
    // Response length should not be null
    assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
}
Also used : Response(feign.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)471 Response (okhttp3.Response)444 MockResponse (okhttp3.mockwebserver.MockResponse)380 Request (okhttp3.Request)377 ResponseBody (okhttp3.ResponseBody)351 IOException (java.io.IOException)220 DateTime (org.joda.time.DateTime)194 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)192 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)178 Response (retrofit2.Response)150 ServiceCall (com.microsoft.rest.ServiceCall)140 ServiceResponse (com.microsoft.rest.ServiceResponse)114 Observable (rx.Observable)104 Call (okhttp3.Call)103 List (java.util.List)95 RequestBody (okhttp3.RequestBody)85 PagedList (com.microsoft.azure.PagedList)80 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)78 OkHttpClient (okhttp3.OkHttpClient)78 HttpURLConnection (java.net.HttpURLConnection)47