use of retrofit2.Callback in project Gladys-Android-App by LeptitGeek.
the class TimelineFragment method createEvent.
public void createEvent(final String name) {
SharedPreferences pref1 = PreferenceManager.getDefaultSharedPreferences(getContext());
pref_house = pref1.getString("idhouse", "1");
pref_user = pref1.getString("iduser", "1");
getConnection();
if (connection) {
getCode(name);
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).client(SelfSigningClientBuilder.getUnsafeOkHttpClient()).build();
RetrofitAPI service = retrofit.create(RetrofitAPI.class);
Call<Event> call = service.createEvents(code, pref_house, pref_user, pref_token);
call.enqueue(new Callback<Event>() {
@Override
public void onResponse(Call<Event> call, Response<Event> response) {
if (response.code() == 201) {
getAllEvents();
if (getActivity() != null) {
SnackbarUtils.simpleSnackBar(getContext(), getView(), getContext().getString(R.string.event_created));
}
} else {
if (getActivity() != null) {
SnackbarUtils.simpleSnackBar(getContext(), getView(), getContext().getString(R.string.error_code_4));
}
}
}
@Override
public void onFailure(Call<Event> call, Throwable t) {
if (getActivity() != null) {
SnackbarUtils.simpleSnackBar(getContext(), getView(), getContext().getString(R.string.error_code_4));
}
}
});
}
}
use of retrofit2.Callback in project syndesis by syndesisio.
the class KubernetesSupport method watchLog.
/*
* Feeds the controller of the given podName to the callback handler for processing.
*
* We do this instead of using the watchLog() feature of the k8s client lib because it really sucks due to:
* 1. You can't configure the timestamps option or the sinceTime option. Need to resume log downloads.
* 2. It seems to need extra threads..
* 3. It might be hiding some of the http failure conditions.
*
*/
protected void watchLog(String podName, Consumer<InputStream> handler, String sinceTime, Executor executor) throws IOException {
try {
PodOperationsImpl pod = (PodOperationsImpl) client.pods().withName(podName);
StringBuilder url = new StringBuilder().append(pod.getResourceUrl().toString()).append("/log?pretty=false&follow=true×tamps=true");
if (sinceTime != null) {
url.append("&sinceTime=");
}
Request request = new Request.Builder().url(new URL(url.toString())).get().build();
OkHttpClient clone = okHttpClient.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
clone.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
LOG.info("Failure occurred getting controller for pod: {},", podName, e);
handler.accept(null);
}
@Override
public void onResponse(final Call call, final Response response) throws IOException {
executor.execute(() -> {
try {
if (response.code() == 200) {
handler.accept(response.body().byteStream());
} else {
LOG.info("Failure occurred while processing controller for pod: {}, http status: {}, details: {}", podName, response.code(), response.body().string());
handler.accept(null);
}
} catch (IOException e) {
LOG.error("Unexpected Error", e);
}
});
}
});
} catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException t) {
throw new IOException("Unexpected Error", t);
}
}
use of retrofit2.Callback in project resilience4j by resilience4j.
the class EnqueueDecorator method enqueue.
public static <T> Response<T> enqueue(Call<T> call) throws Throwable {
final CountDownLatch enqueueLatch = new CountDownLatch(1);
final AtomicReference<Response<T>> responseReference = new AtomicReference<>();
final AtomicReference<Throwable> failureReference = new AtomicReference<>();
call.enqueue(new Callback<T>() {
@Override
public void onResponse(final Call<T> call, final Response<T> response) {
responseReference.set(response);
enqueueLatch.countDown();
}
@Override
public void onFailure(final Call<T> call, final Throwable t) {
failureReference.set(t);
enqueueLatch.countDown();
}
});
enqueueLatch.await();
if (failureReference.get() != null) {
throw failureReference.get();
}
return responseReference.get();
}
use of retrofit2.Callback in project edx-app-android by edx.
the class DownloadSpeedService method performDownload.
private synchronized void performDownload(DownloadDescriptor file) {
final long startTime;
try {
startTime = System.nanoTime();
OkHttpClient client = okHttpClientProvider.getNonOAuthBased().newBuilder().connectTimeout(getResources().getInteger(R.integer.speed_test_timeout_in_milliseconds), TimeUnit.MILLISECONDS).build();
Request request = new Request.Builder().url(file.getUrl()).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException throwable) {
logger.error(throwable);
// If it times out, set a low value for download speed
setCurrentDownloadSpeed(0.01f);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
logger.debug("Download Speed Test Failed");
} else {
long length = response.body().string().length();
double seconds = (System.nanoTime() - startTime) / NS_PER_SEC;
if (seconds != 0) {
final float downloadSpeedKps = (float) ((length / seconds) / 1024);
setCurrentDownloadSpeed(downloadSpeedKps);
reportDownloadSpeed(downloadSpeedKps);
}
}
}
});
} catch (Exception ex) {
logger.error(ex);
}
}
use of retrofit2.Callback in project coolweather by yeliheng.
the class AutoUpdateService method updateBingPic.
/*
* 更新必应每日一图
* */
private void updateBingPic() {
// 必应图片接口
String requestBingPic = "http://guolin.tech/api/bing_pic";
HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String bingPic = response.body().string();
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
// 将从服务器获取到的图片地址存储到SD卡
editor.putString("bing_pic", bingPic);
editor.apply();
}
});
}
Aggregations