Search in sources :

Example 41 with Callback

use of retrofit2.Callback in project Pokemap by omkarmoghe.

the class NianticManager method requestToken.

private void requestToken(String code, final LoginListener loginListener) {
    Log.d(TAG, "requestToken() called with: code = [" + code + "]");
    HttpUrl url = HttpUrl.parse(LOGIN_OAUTH).newBuilder().addQueryParameter("client_id", CLIENT_ID).addQueryParameter("redirect_uri", REDIRECT_URI).addQueryParameter("client_secret", CLIENT_SECRET).addQueryParameter("grant_type", "refresh_token").addQueryParameter("code", code).build();
    Callback<ResponseBody> authCallback = new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                String token = response.body().string().split("token=")[1];
                if (token != null) {
                    token = token.split("&")[0];
                    loginListener.authSuccessful(token);
                } else {
                    Log.e(TAG, "PTC login failed while fetching a requestToken via requestToken(). Token is null.");
                    loginListener.authFailed("Pokemon Trainer Club Login Failed");
                }
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "PTC login failed while fetching a requestToken authCallback.onResponse() raised: " + e.getMessage());
                loginListener.authFailed("Pokemon Trainer Club Authentication Failed");
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.printStackTrace();
            Log.e(TAG, "PTC login failed while fetching a requestToken authCallback.onResponse() threw: " + t.getMessage());
            loginListener.authFailed("Pokemon Trainer Club Authentication Failed");
        }
    };
    Call<ResponseBody> call = mNianticService.requestToken(url.toString());
    call.enqueue(authCallback);
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) ResponseBody(okhttp3.ResponseBody)

Example 42 with Callback

use of retrofit2.Callback in project Shuttle by timusus.

the class DialogUtils method showBiographyDialog.

public static void showBiographyDialog(final Context context, @BioType int type, String artistName, String albumName) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View customView = inflater.inflate(R.layout.dialog_biography, null, false);
    final ProgressBar progressBar = (ProgressBar) customView.findViewById(R.id.progress);
    final TextView message = (TextView) customView.findViewById(R.id.message);
    final ScrollView scrollView = (ScrollView) customView.findViewById(R.id.scrollView);
    ThemeUtils.themeScrollView(scrollView);
    Callback<LastFmArtist> artistCallback = new Callback<LastFmArtist>() {

        @Override
        public void onResponse(Call<LastFmArtist> call, Response<LastFmArtist> response) {
            progressBar.setVisibility(View.GONE);
            if (response != null && response.isSuccessful()) {
                if (response.body() != null && response.body().artist != null && response.body().artist.bio != null) {
                    message.setText(Html.fromHtml(response.body().artist.bio.summary));
                } else {
                    message.setText(R.string.no_artist_info);
                }
            }
        }

        @Override
        public void onFailure(Call<LastFmArtist> call, Throwable t) {
            progressBar.setVisibility(View.GONE);
            switch(type) {
                case BioType.ARTIST:
                    message.setText(R.string.no_artist_info);
                    break;
                case BioType.ALBUM:
                    message.setText(R.string.no_album_info);
                    break;
            }
        }
    };
    Callback<LastFmAlbum> albumCallback = new Callback<LastFmAlbum>() {

        @Override
        public void onResponse(Call<LastFmAlbum> call, Response<LastFmAlbum> response) {
            progressBar.setVisibility(View.GONE);
            if (response != null && response.isSuccessful()) {
                if (response.body() != null && response.body().album != null && response.body().album.wiki != null) {
                    message.setText(Html.fromHtml(response.body().album.wiki.summary));
                } else {
                    message.setText(R.string.no_album_info);
                }
            }
        }

        @Override
        public void onFailure(Call<LastFmAlbum> call, Throwable t) {
            progressBar.setVisibility(View.GONE);
            switch(type) {
                case BioType.ARTIST:
                    message.setText(R.string.no_artist_info);
                    break;
                case BioType.ALBUM:
                    message.setText(R.string.no_album_info);
                    break;
            }
        }
    };
    switch(type) {
        case BioType.ARTIST:
            HttpClient.getInstance().lastFmService.getLastFmArtistResult(artistName).enqueue(artistCallback);
            break;
        case BioType.ALBUM:
            HttpClient.getInstance().lastFmService.getLastFmAlbumResult(artistName, albumName).enqueue(albumCallback);
            break;
    }
    MaterialDialog.Builder builder = getBuilder(context).title(R.string.info).customView(customView, false).negativeText(R.string.close);
    Dialog dialog = builder.show();
    dialog.setOnDismissListener(dialog1 -> {
    });
}
Also used : Call(retrofit2.Call) LastFmArtist(com.simplecity.amp_library.lastfm.LastFmArtist) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) LastFmAlbum(com.simplecity.amp_library.lastfm.LastFmAlbum) View(android.view.View) BlacklistView(com.simplecity.amp_library.ui.modelviews.BlacklistView) ColorView(com.simplecity.amp_library.ui.modelviews.ColorView) TextView(android.widget.TextView) ListView(android.widget.ListView) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) WhitelistView(com.simplecity.amp_library.ui.modelviews.WhitelistView) RecyclerView(android.support.v7.widget.RecyclerView) ScrollView(android.widget.ScrollView) Response(retrofit2.Response) Callback(retrofit2.Callback) ScrollView(android.widget.ScrollView) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Dialog(android.app.Dialog) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) ProgressBar(android.widget.ProgressBar)

Example 43 with Callback

use of retrofit2.Callback in project retrofit by square.

the class CallTest method conversionProblemOutgoingAsync.

@Test
public void conversionProblemOutgoingAsync() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory() {

        @Override
        public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
            return new Converter<String, RequestBody>() {

                @Override
                public RequestBody convert(String value) throws IOException {
                    throw new UnsupportedOperationException("I am broken!");
                }
            };
        }
    }).build();
    Service example = retrofit.create(Service.class);
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.postString("Hi").enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    assertThat(failureRef.get()).isInstanceOf(UnsupportedOperationException.class).hasMessage("I am broken!");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 44 with Callback

use of retrofit2.Callback in project retrofit by square.

the class CallTest method conversionProblemIncomingAsync.

@Test
public void conversionProblemIncomingAsync() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory() {

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            return new Converter<ResponseBody, String>() {

                @Override
                public String convert(ResponseBody value) throws IOException {
                    throw new UnsupportedOperationException("I am broken!");
                }
            };
        }
    }).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("Hi"));
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.postString("Hi").enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    assertThat(failureRef.get()).isInstanceOf(UnsupportedOperationException.class).hasMessage("I am broken!");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 45 with Callback

use of retrofit2.Callback in project retrofit by square.

the class CallTest method http404Async.

@Test
public void http404Async() throws InterruptedException, IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setResponseCode(404).setBody("Hi"));
    final AtomicReference<Response<String>> responseRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.getString().enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            responseRef.set(response);
            latch.countDown();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            t.printStackTrace();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    Response<String> response = responseRef.get();
    assertThat(response.isSuccessful()).isFalse();
    assertThat(response.code()).isEqualTo(404);
    assertThat(response.errorBody().string()).isEqualTo("Hi");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MockResponse(okhttp3.mockwebserver.MockResponse) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)51 Response (okhttp3.Response)46 Call (okhttp3.Call)44 Callback (okhttp3.Callback)44 Request (okhttp3.Request)41 RequestBody (okhttp3.RequestBody)25 Call (retrofit2.Call)17 Callback (retrofit2.Callback)15 Test (org.junit.Test)14 Response (retrofit2.Response)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 FormBody (okhttp3.FormBody)12 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)12 OkHttpClient (okhttp3.OkHttpClient)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ResponseBody (okhttp3.ResponseBody)10 MockResponse (okhttp3.mockwebserver.MockResponse)10 Retrofit (retrofit2.Retrofit)9 TextView (android.widget.TextView)6 HttpUrl (okhttp3.HttpUrl)6