Search in sources :

Example 6 with RetrofitError

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

use of retrofit.RetrofitError in project mortar by square.

the class Chat method getMessages.

public Observable<Message> getMessages() {
    return Observable.create(new Observable.OnSubscribe<Message>() {

        @Override
        public void call(final Subscriber<? super Message> subscriber) {
            final Random random = new Random();
            while (true) {
                if (random.nextInt(PROBABILITY) == 0) {
                    try {
                        User from = users.get(random.nextInt(users.size()));
                        Message next = new Message(from, chats.service.getQuote().quote);
                        messages.add(next);
                        if (!subscriber.isUnsubscribed()) {
                            subscriber.onNext(next);
                        }
                    } catch (RetrofitError e) {
                        if (!subscriber.isUnsubscribed()) {
                            subscriber.onError(e);
                            break;
                        }
                    }
                }
                try {
                    // Hijacking the thread like this is sleazey, but you get the idea.
                    Thread.sleep(SLEEP_MILLIS);
                } catch (InterruptedException e) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onError(e);
                    }
                    break;
                }
            }
        }
    }).startWith(//
    messages).subscribeOn(//
    Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
Also used : Random(java.util.Random) Subscriber(rx.Subscriber) RetrofitError(retrofit.RetrofitError)

Example 8 with RetrofitError

use of retrofit.RetrofitError in project cw-omnibus by commonsguy.

the class SOTests method fetchQuestions.

@Test(timeout = 30000)
public void fetchQuestions() throws InterruptedException {
    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://api.stackexchange.com").build();
    StackOverflowInterface so = restAdapter.create(StackOverflowInterface.class);
    so.questions("android", new Callback<SOQuestions>() {

        @Override
        public void success(SOQuestions soQuestions, Response response) {
            questions = soQuestions;
            responseLatch.countDown();
        }

        @Override
        public void failure(RetrofitError error) {
            responseLatch.countDown();
        }
    });
    responseLatch.await();
    Assert.assertNotNull(questions);
    Assert.assertEquals(30, questions.items.size());
    for (Item item : questions.items) {
        Assert.assertNotNull(item.title);
        Assert.assertNotNull(item.link);
    }
}
Also used : Response(retrofit.client.Response) RestAdapter(retrofit.RestAdapter) RetrofitError(retrofit.RetrofitError) Test(org.junit.Test)

Aggregations

RetrofitError (retrofit.RetrofitError)8 Response (retrofit.client.Response)7 Callback (retrofit.Callback)3 Location (android.location.Location)2 RestAdapter (retrofit.RestAdapter)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 MenuItem (android.view.MenuItem)1 AsynchronousApi (com.kdubb.retrofitexamples.api.AsynchronousApi)1 InterestingApi (com.kdubb.retrofitexamples.api.InterestingApi)1 CheckInSearchAdapter (cz.destil.glasquare.adapter.CheckInSearchAdapter)1 VenuesAdapter (cz.destil.glasquare.adapter.VenuesAdapter)1 CheckIns (cz.destil.glasquare.api.CheckIns)1 ExploreVenues (cz.destil.glasquare.api.ExploreVenues)1 Photos (cz.destil.glasquare.api.Photos)1 SearchVenues (cz.destil.glasquare.api.SearchVenues)1 BaseAsyncTask (cz.destil.glasquare.util.BaseAsyncTask)1 LocationUtils (cz.destil.glasquare.util.LocationUtils)1 Random (java.util.Random)1 Test (org.junit.Test)1 TypedFile (retrofit.mime.TypedFile)1