Search in sources :

Example 6 with RestAdapter

use of retrofit.RestAdapter 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 7 with RestAdapter

use of retrofit.RestAdapter 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)

Example 8 with RestAdapter

use of retrofit.RestAdapter in project retrofit-examples by kdubb1337.

the class SimpleClient method main.

public static void main(String[] args) {
    // Build the Retrofit REST adaptor pointing to the URL specified
    RestAdapter restAdapter = new RestAdapter.Builder().setServer(API_URL).build();
    // Create an instance of our SimpleApi interface.
    SimpleApi simpleApi = restAdapter.create(SimpleApi.class);
    // Call each of the methods and output the results
    System.out.println("simpleApi.simpleGet()=<<" + simpleApi.simpleGet() + ">>");
    System.out.println("simpleApi.simplePost()=<<" + simpleApi.simplePost() + ">>");
    System.out.println("simpleApi.simpleDelete()=<<" + simpleApi.simpleDelete() + ">>");
    System.out.println("simpleApi.simplePut()=<<" + simpleApi.simplePut() + ">>");
    System.out.println("simpleApi.simpleHead()=<<" + simpleApi.simpleHead() + ">>");
    System.out.println("simpleApi.simpleBoolean()=<<" + simpleApi.simpleBoolean() + ">>");
    System.out.println("simpleApi.simpleInteger()=<<" + simpleApi.simpleInteger() + ">>");
    System.out.println("simpleApi.simpleCollection()=<<" + simpleApi.simpleCollection() + ">>");
    try {
        ObjectMapper mapper = new ObjectMapper();
        // We want to take a peek to see if all the fields are set correctly
        CustomObject custom = simpleApi.simpleCustom();
        System.out.println("simpleApi.simpleCustom()=<<" + custom + ">>");
        System.out.println("simpleApi.simpleCustom() as JSON=<<" + mapper.writeValueAsString(custom) + ">>");
        CustomChild child = simpleApi.simpleChild();
        System.out.println("simpleApi.simpleChild()=<<" + child + ">>");
        System.out.println("simpleApi.simpleChild() as JSON=<<" + mapper.writeValueAsString(child) + ">>");
    } catch (IOException e) {
        LOG.error("Failed to convert objects to JSON", e);
    }
}
Also used : CustomObject(com.kdubb.retrofitexamples.domain.CustomObject) SimpleApi(com.kdubb.retrofitexamples.api.SimpleApi) IOException(java.io.IOException) RestAdapter(retrofit.RestAdapter) CustomChild(com.kdubb.retrofitexamples.domain.CustomChild) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 9 with RestAdapter

use of retrofit.RestAdapter in project Android-Week-View by alamkanak.

the class AsynchronousActivity method onMonthChange.

@Override
public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
    // downloaded using retrofit, visit http://square.github.io/retrofit
    if (!calledNetwork) {
        RestAdapter retrofit = new RestAdapter.Builder().setEndpoint("https://api.myjson.com/bins").build();
        MyJsonService service = retrofit.create(MyJsonService.class);
        service.listEvents(this);
        calledNetwork = true;
    }
    // Return only the events that matches newYear and newMonth.
    List<WeekViewEvent> matchedEvents = new ArrayList<WeekViewEvent>();
    for (WeekViewEvent event : events) {
        if (eventMatches(event, newYear, newMonth)) {
            matchedEvents.add(event);
        }
    }
    return matchedEvents;
}
Also used : MyJsonService(com.alamkanak.weekview.sample.apiclient.MyJsonService) ArrayList(java.util.ArrayList) RestAdapter(retrofit.RestAdapter) WeekViewEvent(com.alamkanak.weekview.WeekViewEvent)

Example 10 with RestAdapter

use of retrofit.RestAdapter in project enroscar by stanfy.

the class RetrofitClientTest method shouldWorkForContentScheme.

// content
@Test
public void shouldWorkForContentScheme() throws Exception {
    String baseUri = "content://test.authority";
    RestAdapter adapter = getRestAdapter(baseUri);
    fetchAndTest(adapter);
    Uri uri = Uri.parse(baseUri + "/thing?name=test");
    verify(mockResolver).getType(uri);
    verify(mockResolver).openAssetFileDescriptor(uri, "r");
}
Also used : RestAdapter(retrofit.RestAdapter) Uri(android.net.Uri) Test(org.junit.Test)

Aggregations

RestAdapter (retrofit.RestAdapter)16 View (android.view.View)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 Test (org.junit.Test)3 OkClient (retrofit.client.OkClient)3 ImageView (android.widget.ImageView)2 InterestingApi (com.kdubb.retrofitexamples.api.InterestingApi)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 RetrofitError (retrofit.RetrofitError)2 Response (retrofit.client.Response)2 GsonConverter (retrofit.converter.GsonConverter)2 Uri (android.net.Uri)1 WeekViewEvent (com.alamkanak.weekview.WeekViewEvent)1 MyJsonService (com.alamkanak.weekview.sample.apiclient.MyJsonService)1 Gson (com.google.gson.Gson)1 AsynchronousApi (com.kdubb.retrofitexamples.api.AsynchronousApi)1 SimpleApi (com.kdubb.retrofitexamples.api.SimpleApi)1 JacksonConverter (com.kdubb.retrofitexamples.converter.JacksonConverter)1 CustomChild (com.kdubb.retrofitexamples.domain.CustomChild)1