Search in sources :

Example 1 with AsyncHttpPost

use of com.koushikdutta.async.http.AsyncHttpPost 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 AsyncHttpPost

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

the class HttpServerTests method testString.

public void testString() throws Exception {
    StringBody body = new StringBody("bar");
    AsyncHttpPost post = new AsyncHttpPost("http://localhost:5000/echo");
    post.setBody(body);
    JSONObject json = AsyncHttpClient.getDefaultInstance().executeJSONObject(post, null).get();
    assertEquals(json.getString("foo"), "bar");
}
Also used : JSONObject(org.json.JSONObject) StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost)

Example 3 with AsyncHttpPost

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

the class MultipartTests method testUpload.

public void testUpload() throws Exception {
    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);
}
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 4 with AsyncHttpPost

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

Example 5 with AsyncHttpPost

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

the class HttpClientTests method testPostJsonObject.

public void testPostJsonObject() throws Exception {
    JSONObject post = new JSONObject();
    post.put("ping", "pong");
    AsyncHttpPost p = new AsyncHttpPost("https://koush.clockworkmod.com/test/echo");
    p.setBody(new JSONObjectBody(post));
    JSONObject ret = AsyncHttpClient.getDefaultInstance().executeJSONObject(p, null).get();
    assertEquals("pong", ret.getString("ping"));
}
Also used : JSONObjectBody(com.koushikdutta.async.http.body.JSONObjectBody) JSONObject(org.json.JSONObject) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost)

Aggregations

AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)8 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)3 JSONObjectBody (com.koushikdutta.async.http.body.JSONObjectBody)3 StringBody (com.koushikdutta.async.http.body.StringBody)3 JSONObject (org.json.JSONObject)3 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)2 AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)2 File (java.io.File)2 Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Uri (android.net.Uri)1 ImageView (android.widget.ImageView)1 StringCallback (com.koushikdutta.async.http.AsyncHttpClient.StringCallback)1 BasicNameValuePair (com.koushikdutta.async.http.BasicNameValuePair)1 NameValuePair (com.koushikdutta.async.http.NameValuePair)1 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)1 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1