Search in sources :

Example 6 with AsyncHttpResponse

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

the class MultipartTests method testUpload.

public void testUpload() throws Exception {
    setUp();
    try {
        File dummy = getContext().getFileStreamPath("dummy.txt");
        final String FIELD_VAL = "bar";
        dummy.getParentFile().mkdirs();
        FileOutputStream fout = new FileOutputStream(dummy);
        byte[] zeroes = new byte[100000];
        for (int i = 0; i < 10; i++) {
            fout.write(zeroes);
        }
        fout.close();
        // StreamUtility.writeFile(dummy, DUMMY_VAL);
        AsyncHttpPost post = new AsyncHttpPost("http://localhost:5000");
        MultipartFormDataBody body = new MultipartFormDataBody();
        body.addStringPart("foo", FIELD_VAL);
        body.addFilePart("my-file", dummy);
        body.addStringPart("baz", FIELD_VAL);
        post.setBody(body);
        Future<String> ret = AsyncHttpClient.getDefaultInstance().executeString(post, new StringCallback() {

            @Override
            public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
            }
        });
        String data = ret.get(10000, TimeUnit.MILLISECONDS);
        assertEquals(data, FIELD_VAL + (zeroes.length * 10) + FIELD_VAL);
    } finally {
        tearDown();
    }
}
Also used : AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) FileOutputStream(java.io.FileOutputStream) StringCallback(com.koushikdutta.async.http.AsyncHttpClient.StringCallback) File(java.io.File) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 7 with AsyncHttpResponse

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

the class AsyncProxyServer method onRequest.

@Override
protected void onRequest(HttpServerRequestCallback callback, AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
    super.onRequest(callback, request, response);
    if (callback != null)
        return;
    try {
        Uri uri;
        try {
            uri = Uri.parse(request.getPath());
            if (uri.getScheme() == null)
                throw new Exception("no host or full uri provided");
        } catch (Exception e) {
            String host = request.getHeaders().get("Host");
            int port = 80;
            if (host != null) {
                String[] splits = host.split(":", 2);
                if (splits.length == 2) {
                    host = splits[0];
                    port = Integer.parseInt(splits[1]);
                }
            }
            uri = Uri.parse("http://" + host + ":" + port + request.getPath());
        }
        proxyClient.execute(new AsyncHttpRequest(uri, request.getMethod(), request.getHeaders()), new HttpConnectCallback() {

            @Override
            public void onConnectCompleted(Exception ex, AsyncHttpResponse remoteResponse) {
                if (ex != null) {
                    response.code(500);
                    response.send(ex.getMessage());
                    return;
                }
                response.proxy(remoteResponse);
            }
        });
    } catch (Exception e) {
        response.code(500);
        response.send(e.getMessage());
    }
}
Also used : AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) Uri(android.net.Uri) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Example 8 with AsyncHttpResponse

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

the class MainActivity method getChartFile.

private void getChartFile() {
    final ImageView iv = chart;
    final String filename = getFileStreamPath(randomFile()).getAbsolutePath();
    ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("cht", "lc"));
    pairs.add(new BasicNameValuePair("chtt", "This is a google chart"));
    pairs.add(new BasicNameValuePair("chs", "512x512"));
    pairs.add(new BasicNameValuePair("chxt", "x"));
    pairs.add(new BasicNameValuePair("chd", "t:40,20,50,20,100"));
    UrlEncodedFormBody writer = new UrlEncodedFormBody(pairs);
    try {
        AsyncHttpPost post = new AsyncHttpPost("http://chart.googleapis.com/chart");
        post.setBody(writer);
        AsyncHttpClient.getDefaultInstance().executeFile(post, filename, new AsyncHttpClient.FileCallback() {

            @Override
            public void onCompleted(Exception e, AsyncHttpResponse response, File result) {
                if (e != null) {
                    e.printStackTrace();
                    return;
                }
                Bitmap bitmap = BitmapFactory.decodeFile(filename);
                result.delete();
                if (bitmap == null)
                    return;
                BitmapDrawable bd = new BitmapDrawable(bitmap);
                assignImageView(iv, bd);
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : BasicNameValuePair(com.koushikdutta.async.http.BasicNameValuePair) NameValuePair(com.koushikdutta.async.http.NameValuePair) ArrayList(java.util.ArrayList) BitmapDrawable(android.graphics.drawable.BitmapDrawable) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) BasicNameValuePair(com.koushikdutta.async.http.BasicNameValuePair) ImageView(android.widget.ImageView) UrlEncodedFormBody(com.koushikdutta.async.http.body.UrlEncodedFormBody) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost) File(java.io.File) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

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