aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/Util.java3
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java3
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/http/ResponseCacheMiddleware.java19
-rw-r--r--AndroidAsyncSample/src/com/koushikdutta/async/sample/MainActivity.java14
-rw-r--r--README.md38
5 files changed, 65 insertions, 12 deletions
diff --git a/AndroidAsync/src/com/koushikdutta/async/Util.java b/AndroidAsync/src/com/koushikdutta/async/Util.java
index 14bdf69..beced7e 100644
--- a/AndroidAsync/src/com/koushikdutta/async/Util.java
+++ b/AndroidAsync/src/com/koushikdutta/async/Util.java
@@ -24,6 +24,9 @@ public class Util {
}
}
if (list.remaining() != 0 && !emitter.isPaused()) {
+ // not all the data was consumed...
+ // call byteBufferList.clear() or read all the data to prevent this assertion.
+ // this is nice to have, as it identifies protocol or parsing errors.
System.out.println("Data: " + list.peekString());
System.out.println("handler: " + handler);
Assert.fail();
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java
index 1fd3ece..fef07a3 100644
--- a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java
+++ b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java
@@ -46,6 +46,9 @@ public class AsyncHttpClient {
}
ArrayList<AsyncHttpClientMiddleware> mMiddleware = new ArrayList<AsyncHttpClientMiddleware>();
+ public ArrayList<AsyncHttpClientMiddleware> getMiddleware() {
+ return mMiddleware;
+ }
public void insertMiddleware(AsyncHttpClientMiddleware middleware) {
synchronized (mMiddleware) {
mMiddleware.add(0, middleware);
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/ResponseCacheMiddleware.java b/AndroidAsync/src/com/koushikdutta/async/http/ResponseCacheMiddleware.java
index 1f87533..1af857a 100644
--- a/AndroidAsync/src/com/koushikdutta/async/http/ResponseCacheMiddleware.java
+++ b/AndroidAsync/src/com/koushikdutta/async/http/ResponseCacheMiddleware.java
@@ -61,13 +61,18 @@ public class ResponseCacheMiddleware extends SimpleMiddleware {
private static final int ENTRY_COUNT = 2;
private AsyncHttpClient client;
- public ResponseCacheMiddleware(AsyncHttpClient client, File cacheDir) {
- try {
- this.client = client;
- cache = DiskLruCache.open(cacheDir, VERSION, ENTRY_COUNT, 1024L * 1024L * 10L);
- }
- catch (IOException e) {
- }
+ private ResponseCacheMiddleware() {
+ }
+
+ public static ResponseCacheMiddleware addCache(AsyncHttpClient client, File cacheDir, long size) throws IOException {
+ for (AsyncHttpClientMiddleware middleware: client.getMiddleware()) {
+ if (middleware instanceof ResponseCacheMiddleware)
+ throw new IOException("Response cache already added to http client");
+ }
+ ResponseCacheMiddleware ret = new ResponseCacheMiddleware();
+ ret.client = client;
+ ret.cache = DiskLruCache.open(cacheDir, VERSION, ENTRY_COUNT, size);
+ return ret;
}
boolean caching = true;
diff --git a/AndroidAsyncSample/src/com/koushikdutta/async/sample/MainActivity.java b/AndroidAsyncSample/src/com/koushikdutta/async/sample/MainActivity.java
index a7efd0b..115f312 100644
--- a/AndroidAsyncSample/src/com/koushikdutta/async/sample/MainActivity.java
+++ b/AndroidAsyncSample/src/com/koushikdutta/async/sample/MainActivity.java
@@ -1,6 +1,7 @@
package com.koushikdutta.async.sample;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
@@ -27,7 +28,6 @@ import com.koushikdutta.async.http.ResponseCacheMiddleware;
import com.koushikdutta.async.http.UrlEncodedFormBody;
public class MainActivity extends Activity {
- static boolean cacheAdded = false;
static ResponseCacheMiddleware cacher;
ImageView rommanager;
@@ -39,10 +39,14 @@ public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- if (!cacheAdded) {
- cacheAdded = true;
- AsyncHttpClient.getDefaultInstance().insertMiddleware(cacher = new ResponseCacheMiddleware(AsyncHttpClient.getDefaultInstance(), getFileStreamPath("asynccache")));
- cacher.setCaching(false);
+ if (cacher == null) {
+ try {
+ cacher = ResponseCacheMiddleware.addCache(AsyncHttpClient.getDefaultInstance(), getFileStreamPath("asynccache"), 1024 * 1024 * 10);
+ cacher.setCaching(false);
+ }
+ catch (IOException e) {
+ Toast.makeText(getApplicationContext(), "unable to create cache", Toast.LENGTH_SHORT).show();
+ }
}
setContentView(R.layout.activity_main);
diff --git a/README.md b/README.md
index bf7dbdb..ee15309 100644
--- a/README.md
+++ b/README.md
@@ -45,6 +45,44 @@ AsyncHttpClient.getDefaultInstance().get(url, filename, new AsyncHttpClient.File
+### Caching is supported too (experimental)
+
+```java
+// arguments are the http client, the directory to store the cache, and the maximum size of the cache
+ResponseCacheMiddleware.addCache(AsyncHttpClient.getDefaultInstance(), getFileStreamPath("asynccache"), 1024 * 1024 * 10);
+```
+
+
+### Can also create web sockets:
+
+```java
+AsyncHttpClient.getDefaultInstance().websocket(get, "my-protocol", new WebSocketConnectCallback() {
+ @Override
+ public void onCompleted(Exception ex, WebSocket webSocket) {
+ if (ex != null) {
+ ex.printStackTrace();
+ return;
+ }
+
+ webSocket.send("a string");
+ webSocket.send(new byte[10]);
+
+ webSocket.setStringCallback(new StringCallback() {
+ public void onStringAvailable(String s) {
+ System.out.println("I got a string: " + s);
+ }
+ });
+
+ webSocket.setDataCallback(new DataCallback() {
+ public void onDataAvailable(ByteBufferList byteBufferList) {
+ System.out.println("I got some bytes!");
+ // note that this data has been read
+ byteBufferList.clear();
+ }
+ });
+ }
+});
+```
### AndroidAsync also let's you create simple HTTP servers: