use of com.koushikdutta.async.future.FutureCallback in project AndroidAsync by koush.
the class AsyncHttpClient method execute.
public <T> SimpleFuture<T> execute(AsyncHttpRequest req, final AsyncParser<T> parser, final RequestCallback<T> callback) {
final FutureAsyncHttpResponse cancel = new FutureAsyncHttpResponse();
final SimpleFuture<T> ret = new SimpleFuture<T>();
execute(req, 0, cancel, new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
if (ex != null) {
invoke(callback, ret, response, ex, null);
return;
}
invokeConnect(callback, response);
Future<T> parsed = parser.parse(response).setCallback(new FutureCallback<T>() {
@Override
public void onCompleted(Exception e, T result) {
invoke(callback, ret, response, e, result);
}
});
// reparent to the new parser future
ret.setParent(parsed);
}
});
ret.setParent(cancel);
return ret;
}
use of com.koushikdutta.async.future.FutureCallback in project AndroidAsync by koush.
the class HttpClientTests method testFileCancel.
public void testFileCancel() throws Exception {
final Semaphore semaphore = new Semaphore(0);
File f = getContext().getFileStreamPath("test.txt");
fileFuture = client.executeFile(new AsyncHttpGet(github), f.getAbsolutePath(), new AsyncHttpClient.FileCallback() {
@Override
public void onCompleted(Exception e, AsyncHttpResponse source, File result) {
fail();
}
@Override
public void onProgress(AsyncHttpResponse response, long downloaded, long total) {
semaphore.release();
}
}).setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File result) {
assertTrue(e instanceof CancellationException);
}
});
try {
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
assertTrue(fileFuture.cancel());
fileFuture.get();
fail();
} catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof CancellationException);
}
// Thread.sleep(1000);
// assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
assertFalse(f.exists());
}
use of com.koushikdutta.async.future.FutureCallback in project ion by koush.
the class Twitter method load.
private void load() {
// don't attempt to load more if a load is already in progress
if (loading != null && !loading.isDone() && !loading.isCancelled())
return;
// load the tweets
String url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=BestAt&count=20";
if (tweetAdapter.getCount() > 0) {
// load from the "last" id
JsonObject last = tweetAdapter.getItem(tweetAdapter.getCount() - 1);
url += "&max_id=" + last.get("id_str").getAsString();
}
// Request tweets from Twitter using Ion.
// This is done using Ion's Fluent/Builder API.
// This API lets you chain calls together to build
// complex requests.
// This request loads a URL as JsonArray and invokes
// a callback on completion.
loading = Ion.with(this).load(url).setHeader("Authorization", "Bearer " + accessToken).asJsonArray().setCallback(new FutureCallback<JsonArray>() {
@Override
public void onCompleted(Exception e, JsonArray result) {
// this is called back onto the ui thread, no Activity.runOnUiThread or Handler.post necessary.
if (e != null) {
Toast.makeText(Twitter.this, "Error loading tweets", Toast.LENGTH_LONG).show();
return;
}
// add the tweets
for (int i = 0; i < result.size(); i++) {
tweetAdapter.add(result.get(i).getAsJsonObject());
}
}
});
}
use of com.koushikdutta.async.future.FutureCallback in project CoCoin by Nightonke.
the class AccountBookListViewActivity method loadLogo.
private void loadLogo() {
User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
if (user != null) {
try {
File logoFile = new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME);
if (!logoFile.exists()) {
// the local logo file is missed
// try to get from the server
BmobQuery<Logo> bmobQuery = new BmobQuery();
bmobQuery.addWhereEqualTo("objectId", user.getLogoObjectId());
bmobQuery.findObjects(CoCoinApplication.getAppContext(), new FindListener<Logo>() {
@Override
public void onSuccess(List<Logo> object) {
// there has been an old logo in the server/////////////////////////////////////////////////////////
String url = object.get(0).getFile().getFileUrl(CoCoinApplication.getAppContext());
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Logo in server: " + url);
Ion.with(CoCoinApplication.getAppContext()).load(url).write(new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME)).setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File file) {
profileImage.setImageBitmap(BitmapFactory.decodeFile(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME));
}
});
}
@Override
public void onError(int code, String msg) {
// the picture is lost
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Can't find the old logo in server.");
}
});
} else {
// the user logo is in the storage
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(logoFile));
profileImage.setImageBitmap(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
// use the default logo
profileImage.setImageResource(R.drawable.default_user_logo);
}
}
use of com.koushikdutta.async.future.FutureCallback in project CoCoin by Nightonke.
the class AccountBookMonthViewActivity method loadLogo.
private void loadLogo() {
User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
if (user != null) {
try {
File logoFile = new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME);
if (!logoFile.exists()) {
// the local logo file is missed
// try to get from the server
BmobQuery<Logo> bmobQuery = new BmobQuery();
bmobQuery.addWhereEqualTo("objectId", user.getLogoObjectId());
bmobQuery.findObjects(CoCoinApplication.getAppContext(), new FindListener<Logo>() {
@Override
public void onSuccess(List<Logo> object) {
// there has been an old logo in the server/////////////////////////////////////////////////////////
String url = object.get(0).getFile().getFileUrl(CoCoinApplication.getAppContext());
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Logo in server: " + url);
Ion.with(CoCoinApplication.getAppContext()).load(url).write(new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME)).setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File file) {
profileImage.setImageBitmap(BitmapFactory.decodeFile(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME));
}
});
}
@Override
public void onError(int code, String msg) {
// the picture is lost
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Can't find the old logo in server.");
}
});
} else {
// the user logo is in the storage
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(logoFile));
profileImage.setImageBitmap(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
// use the default logo
profileImage.setImageResource(R.drawable.default_user_logo);
}
}
Aggregations