Search in sources :

Example 6 with FutureCallback

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;
}
Also used : HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) SimpleFuture(com.koushikdutta.async.future.SimpleFuture) Future(com.koushikdutta.async.future.Future) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FutureCallback(com.koushikdutta.async.future.FutureCallback) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 7 with FutureCallback

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());
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) CancellationException(java.util.concurrent.CancellationException) Semaphore(java.util.concurrent.Semaphore) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.koushikdutta.async.future.FutureCallback) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 8 with FutureCallback

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());
            }
        }
    });
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) FutureCallback(com.koushikdutta.async.future.FutureCallback) Paint(android.graphics.Paint)

Example 9 with FutureCallback

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);
    }
}
Also used : BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) Logo(com.nightonke.saver.model.Logo) Bitmap(android.graphics.Bitmap) BmobQuery(cn.bmob.v3.BmobQuery) File(java.io.File) FutureCallback(com.koushikdutta.async.future.FutureCallback)

Example 10 with FutureCallback

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);
    }
}
Also used : User(com.nightonke.saver.model.User) BmobUser(cn.bmob.v3.BmobUser) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) Logo(com.nightonke.saver.model.Logo) Bitmap(android.graphics.Bitmap) BmobQuery(cn.bmob.v3.BmobQuery) File(java.io.File) FutureCallback(com.koushikdutta.async.future.FutureCallback)

Aggregations

FutureCallback (com.koushikdutta.async.future.FutureCallback)12 File (java.io.File)8 FileNotFoundException (java.io.FileNotFoundException)8 Bitmap (android.graphics.Bitmap)6 BmobQuery (cn.bmob.v3.BmobQuery)6 BmobUser (cn.bmob.v3.BmobUser)6 Logo (com.nightonke.saver.model.Logo)6 User (com.nightonke.saver.model.User)6 FileInputStream (java.io.FileInputStream)5 IOException (java.io.IOException)5 BmobFile (cn.bmob.v3.datatype.BmobFile)3 Future (com.koushikdutta.async.future.Future)2 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Paint (android.graphics.Paint)1 Point (android.graphics.Point)1 BmobProFile (com.bmob.BmobProFile)1 JsonArray (com.google.gson.JsonArray)1