Search in sources :

Example 1 with AsyncHttpResponse

use of com.koushikdutta.async.http.AsyncHttpResponse in project AndroidAsync by koush.

the class XHRPollingTransport method send.

@Override
public void send(String message) {
    if (message.startsWith("5")) {
        postMessage(message);
        return;
    }
    AsyncHttpRequest request = new AsyncHttpPost(computedRequestUrl());
    request.setBody(new StringBody(message));
    client.executeString(request, new AsyncHttpClient.StringCallback() {

        @Override
        public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
            if (e != null) {
                close(e);
                return;
            }
            sendResult(result);
        }
    });
}
Also used : AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 2 with AsyncHttpResponse

use of com.koushikdutta.async.http.AsyncHttpResponse in project AndroidAsync by koush.

the class CacheTests method test304.

@Test
public void test304() throws Exception {
    try {
        AsyncHttpServer httpServer = new AsyncHttpServer();
        AsyncServerSocket socket = httpServer.listen(AsyncServer.getDefault(), 0);
        int port = socket.getLocalPort();
        AssetManager am = InstrumentationRegistry.getTargetContext().getAssets();
        httpServer.directory(InstrumentationRegistry.getTargetContext(), "/.*?", "");
        AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault());
        ByteBufferList bb = client.executeByteBufferList(new AsyncHttpGet("http://localhost:" + port + "/" + dataNameAndHash), new AsyncHttpClient.DownloadCallback() {

            @Override
            public void onCompleted(Exception e, AsyncHttpResponse source, ByteBufferList result) {
                System.out.println(source.headers());
            }
        }).get();
    } finally {
        AsyncServer.getDefault().stop();
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) AssetManager(android.content.res.AssetManager) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient) Test(org.junit.Test)

Example 3 with AsyncHttpResponse

use of com.koushikdutta.async.http.AsyncHttpResponse in project AndroidAsync by koush.

the class HttpClientTests method testClockworkMod.

@Test
public void testClockworkMod() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    client.execute("http://www.clockworkmod.com", new HttpConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            // make sure gzip decoding works, as that is generally what github sends.
            Assert.assertEquals("gzip", response.headers().get("Content-Encoding"));
            response.setDataCallback(new DataCallback() {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    md5.update(bb);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    semaphore.release();
                }
            });
        }
    });
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with AsyncHttpResponse

use of com.koushikdutta.async.http.AsyncHttpResponse 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();
        }
    });
    fileFuture.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)

Example 5 with AsyncHttpResponse

use of com.koushikdutta.async.http.AsyncHttpResponse in project AndroidAsync by koush.

the class HttpClientTests method testGithubRandomData.

public void testGithubRandomData() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    AsyncHttpGet get = new AsyncHttpGet(github);
    get.setLogging("AsyncTest", Log.VERBOSE);
    client.execute(get, new HttpConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            assertNull(ex);
            // make sure gzip decoding works, as that is generally what github sends.
            // this broke sometime in 03/2014
            // Assert.assertEquals("gzip", response.getHeaders().getContentEncoding());
            response.setDataCallback(new DataCallback() {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    md5.update(bb);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    semaphore.release();
                }
            });
        }
    });
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals(md5.digest(), dataNameAndHash);
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)8 ByteBufferList (com.koushikdutta.async.ByteBufferList)3 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)3 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)3 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)3 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)3 File (java.io.File)3 CancellationException (java.util.concurrent.CancellationException)3 ExecutionException (java.util.concurrent.ExecutionException)3 Semaphore (java.util.concurrent.Semaphore)3 TimeoutException (java.util.concurrent.TimeoutException)3 DataEmitter (com.koushikdutta.async.DataEmitter)2 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)2 DataCallback (com.koushikdutta.async.callback.DataCallback)2 AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)2 Test (org.junit.Test)2 AssetManager (android.content.res.AssetManager)1 Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Uri (android.net.Uri)1