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);
}
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 -> {
});
}
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!");
}
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!");
}
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");
}
Aggregations