Search in sources :

Example 1 with InterestingApi

use of com.kdubb.retrofitexamples.api.InterestingApi in project retrofit-examples by kdubb1337.

the class AsynchronousClient method main.

public static void main(String[] args) {
    // Build the Retrofit REST adaptor pointing to the URL specified
    // with a ThrottlingInterceptor allowing only 1 request per second
    RestAdapter restAdapter = new RestAdapter.Builder().setRequestInterceptor(new ThrottlingInterceptor(1000L)).setServer(API_URL).build();
    // Create an instance of our InterestingApi interface.
    InterestingApi synchronousApi = restAdapter.create(InterestingApi.class);
    // Create an instance of our AsynchronousApi interface.
    AsynchronousApi asyncApi = restAdapter.create(AsynchronousApi.class);
    for (int i = 0; i < 10; i++) LOG.info("synchronousApi " + synchronousApi.getWithPath(Integer.toString(i)));
    for (int i = 0; i < 10; i++) asyncApi.getWithPath(Integer.toString(i), new Callback<String>() {

        @Override
        public void success(String t, Response response) {
            LOG.info("asynchronousApi (" + t + ")");
        }

        @Override
        public void failure(RetrofitError error) {
            LOG.info("Epic fail!");
        }
    });
}
Also used : Response(retrofit.client.Response) Callback(retrofit.Callback) AsynchronousApi(com.kdubb.retrofitexamples.api.AsynchronousApi) RestAdapter(retrofit.RestAdapter) InterestingApi(com.kdubb.retrofitexamples.api.InterestingApi) RetrofitError(retrofit.RetrofitError)

Example 2 with InterestingApi

use of com.kdubb.retrofitexamples.api.InterestingApi in project retrofit-examples by kdubb1337.

the class InterestingClient method main.

public static void main(String[] args) {
    // Create our Converter
    JacksonConverter converter = new JacksonConverter(new ObjectMapper());
    // Build the Retrofit REST adaptor pointing to the URL specified, with the Converter.
    // Note: The Converter must be set before the .build() command
    RestAdapter restAdapter = new RestAdapter.Builder().setConverter(converter).setServer(API_URL).build();
    // Create an instance of our InterestingApi interface.
    InterestingApi api = restAdapter.create(InterestingApi.class);
    // Call each of the methods and output the results
    System.out.println("api.getDate()={" + api.getDate() + "}");
    System.out.println("api.getWithPath()={" + api.getWithPath("my String 1234") + "}");
    System.out.println("api.getWithQuery()={" + api.getWithQuery("my String 1234") + "}");
    System.out.println("api.getWithBody()={" + api.getWithBody("my String 1234") + "}");
    System.out.println("api.getWithDynamicHeader()={" + api.getWithDynamicHeader("max-age=26000") + "}");
    System.out.println("api.getWithFixedHeaders()={" + api.getWithFixedHeaders() + "}");
}
Also used : JacksonConverter(com.kdubb.retrofitexamples.converter.JacksonConverter) RestAdapter(retrofit.RestAdapter) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) InterestingApi(com.kdubb.retrofitexamples.api.InterestingApi)

Aggregations

InterestingApi (com.kdubb.retrofitexamples.api.InterestingApi)2 RestAdapter (retrofit.RestAdapter)2 AsynchronousApi (com.kdubb.retrofitexamples.api.AsynchronousApi)1 JacksonConverter (com.kdubb.retrofitexamples.converter.JacksonConverter)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 Callback (retrofit.Callback)1 RetrofitError (retrofit.RetrofitError)1 Response (retrofit.client.Response)1