Search in sources :

Example 1 with AsyncHttpRequest

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

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

the class TimeoutTests method testSlowBody.

public void testSlowBody() throws Exception {
    AsyncHttpRequest req = new AsyncHttpRequest(Uri.parse("http://localhost:5000/now"), "POST");
    req.setTimeout(1000);
    req.setLogging("slowbody", Log.VERBOSE);
    req.setBody(new DelayedStringBody("foo"));
    assertEquals("foo", AsyncHttpClient.getDefaultInstance().executeString(req, null).get());
    req = new AsyncHttpRequest(Uri.parse("http://localhost:5000/3"), "GET");
    req.setLogging("slowbody", Log.VERBOSE);
    req.setTimeout(100);
    req.setBody(new DelayedStringBody("foo"));
    try {
        AsyncHttpClient.getDefaultInstance().executeString(req, null).get();
        fail();
    } catch (Exception e) {
        Log.d("timeout", "error", e);
        assertTrue(e.getCause() instanceof TimeoutException);
    }
}
Also used : AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with AsyncHttpRequest

use of com.koushikdutta.async.http.AsyncHttpRequest in project ion by koush.

the class IonRequestBuilder method execute.

<T> ResponseFuture<T> execute(final AsyncParser<T> parser, Runnable cancel) {
    assert parser != null;
    final Uri uri = prepareURI();
    AsyncHttpRequest request = null;
    if (uri != null) {
        request = prepareRequest(uri);
        Type type = parser.getType();
        for (Loader loader : ion.loaders) {
            ResponseFuture<T> quickLoad = loader.load(ion, request, type);
            if (quickLoad != null)
                return quickLoad;
        }
    }
    EmitterTransform<T> ret = new EmitterTransform<T>(cancel) {

        EmitterTransform<T> self = this;

        @Override
        protected void transform(LoaderEmitter emitter) throws Exception {
            super.transform(emitter);
            parser.parse(this.emitter).setCallback(new FutureCallback<T>() {

                @Override
                public void onCompleted(Exception e, T result) {
                    postExecute(self, e, result);
                }
            });
        }
    };
    if (uri == null) {
        ret.setComplete(new Exception("Invalid URI"));
        return ret;
    }
    ret.initialRequest = request;
    getLoaderEmitter(ret);
    return ret;
}
Also used : Type(java.lang.reflect.Type) Uri(android.net.Uri) LoaderEmitter(com.koushikdutta.ion.Loader.LoaderEmitter) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Example 4 with AsyncHttpRequest

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

the class TimeoutTests method testTimeout.

public void testTimeout() throws Exception {
    AsyncHttpRequest req = new AsyncHttpRequest(Uri.parse("http://localhost:5000/3"), "GET");
    req.setTimeout(1000);
    try {
        AsyncHttpClient.getDefaultInstance().executeString(req, null).get();
        fail();
    } catch (Exception e) {
        Log.d("timeout", "error", e);
        assertTrue(e.getCause() instanceof TimeoutException);
    }
    req = new AsyncHttpRequest(Uri.parse("http://localhost:5000/3"), "GET");
    assertEquals("3", AsyncHttpClient.getDefaultInstance().executeString(req, null).get());
}
Also used : AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with AsyncHttpRequest

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

the class HttpClientTests method testUriPathWithSpaces.

public void testUriPathWithSpaces() throws Exception {
    AsyncHttpRequest request = new AsyncHttpRequest(Uri.parse("http://jpkc.seiee.sjtu.edu.cn/ds/ds2/Course%20lecture/chapter%2010.pdf"), AsyncHttpGet.METHOD);
    String requestLine = request.getRequestLine().toString();
    assertEquals("GET /ds/ds2/Course%20lecture/chapter%2010.pdf HTTP/1.1", requestLine);
}
Also used : AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Aggregations

AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)11 Uri (android.net.Uri)4 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)2 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)2 StringBody (com.koushikdutta.async.http.body.StringBody)2 TimeoutException (java.util.concurrent.TimeoutException)2 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 WebSocketConnectCallback (com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback)1 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)1 Multimap (com.koushikdutta.async.http.Multimap)1 WebSocket (com.koushikdutta.async.http.WebSocket)1 AsyncHttpRequestBody (com.koushikdutta.async.http.body.AsyncHttpRequestBody)1 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)1 LoaderEmitter (com.koushikdutta.ion.Loader.LoaderEmitter)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1