Search in sources :

Example 1 with StringCallback

use of com.koushikdutta.async.http.AsyncHttpClient.StringCallback 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)

Aggregations

StringCallback (com.koushikdutta.async.http.AsyncHttpClient.StringCallback)1 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)1 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)1 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1