Search in sources :

Example 1 with Query

use of retrofit2.http.Query in project retrofit by square.

the class JsonQueryParameters method main.

public static void main(String... args) throws IOException, InterruptedException {
    MockWebServer server = new MockWebServer();
    server.start();
    server.enqueue(new MockResponse());
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new JsonStringConverterFactory(GsonConverterFactory.create())).build();
    Service service = retrofit.create(Service.class);
    Call<ResponseBody> call = service.example(new Filter("123"));
    Response<ResponseBody> response = call.execute();
    // TODO handle user response...
    // Print the request path that the server saw to show the JSON query param:
    RecordedRequest recordedRequest = server.takeRequest();
    System.out.println(recordedRequest.getPath());
    server.shutdown();
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Retrofit(retrofit2.Retrofit) MockWebServer(okhttp3.mockwebserver.MockWebServer) ResponseBody(okhttp3.ResponseBody)

Example 2 with Query

use of retrofit2.http.Query in project retrofit by square.

the class RequestBuilderTest method getWithEncodedQueryParam.

@Test
public void getWithEncodedQueryParam() {
    class Example {

        // 
        @GET("/foo/bar/")
        Call<ResponseBody> method(@Query(value = "pi%20ng", encoded = true) String ping) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "p%20o%20n%20g");
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/?pi%20ng=p%20o%20n%20g");
    assertThat(request.body()).isNull();
}
Also used : Query(retrofit2.http.Query) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 3 with Query

use of retrofit2.http.Query in project retrofit by square.

the class RequestBuilderTest method fieldParamMapsConvertedToNullShouldError.

@Test
public void fieldParamMapsConvertedToNullShouldError() throws Exception {
    class Example {

        @FormUrlEncoded
        @POST("/query")
        Call<ResponseBody> queryPath(@FieldMap Map<String, String> a) {
            return null;
        }
    }
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl("http://example.com").addConverterFactory(new NullObjectConverterFactory());
    Map<String, String> queryMap = Collections.singletonMap("kit", "kat");
    try {
        buildRequest(Example.class, retrofitBuilder, queryMap);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessageContaining("Field map value 'kat' converted to null by retrofit2.helpers.NullObjectConverterFactory$1 for key 'kit'.");
    }
}
Also used : NullObjectConverterFactory(retrofit2.helpers.NullObjectConverterFactory) FieldMap(retrofit2.http.FieldMap) PartMap(retrofit2.http.PartMap) HashMap(java.util.HashMap) HeaderMap(retrofit2.http.HeaderMap) FieldMap(retrofit2.http.FieldMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) QueryMap(retrofit2.http.QueryMap) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 4 with Query

use of retrofit2.http.Query in project retrofit by square.

the class RequestBuilderTest method queryParamsSkippedIfConvertedToNull.

@Test
public void queryParamsSkippedIfConvertedToNull() throws Exception {
    class Example {

        @GET("/query")
        Call<ResponseBody> queryPath(@Query("a") Object a) {
            return null;
        }
    }
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl("http://example.com").addConverterFactory(new NullObjectConverterFactory());
    Request request = buildRequest(Example.class, retrofitBuilder, "Ignored");
    assertThat(request.url().toString()).doesNotContain("Ignored");
}
Also used : NullObjectConverterFactory(retrofit2.helpers.NullObjectConverterFactory) Query(retrofit2.http.Query) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 5 with Query

use of retrofit2.http.Query in project retrofit by square.

the class RequestBuilderTest method queryParamOptional.

@Test
public void queryParamOptional() {
    class Example {

        // 
        @GET("/foo/bar/")
        Call<ResponseBody> method(@Query("foo") String foo, @Query("ping") String ping, @Query("kit") String kit) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "bar", null, "kat");
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/?foo=bar&kit=kat");
}
Also used : Query(retrofit2.http.Query) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)23 Test (org.junit.Test)20 Request (okhttp3.Request)18 Query (retrofit2.http.Query)15 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 Response (retrofit2.Response)4 Retrofit (retrofit2.Retrofit)4 NullObjectConverterFactory (retrofit2.helpers.NullObjectConverterFactory)4 FieldMap (retrofit2.http.FieldMap)4 HeaderMap (retrofit2.http.HeaderMap)4 PartMap (retrofit2.http.PartMap)4 Path (retrofit2.http.Path)4 QueryMap (retrofit2.http.QueryMap)4 ServiceResponse (com.microsoft.rest.ServiceResponse)3 API (com.arm.mbed.cloud.sdk.annotations.API)2 Nullable (com.arm.mbed.cloud.sdk.annotations.Nullable)2 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)2 Query (com.arm.mbed.cloud.sdk.devicedirectory.model.Query)2