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);
}
});
}
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();
}
}
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));
}
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());
}
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);
}
Aggregations