Search in sources :

Example 1 with StringBody

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

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

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

the class HttpServerTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    httpServer = new AsyncHttpServer();
    httpServer.setErrorCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            fail();
        }
    });
    httpServer.listen(AsyncServer.getDefault(), 5000);
    httpServer.get("/hello", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            assertNotNull(request.getHeaders().get("Host"));
            response.send("hello");
        }
    });
    httpServer.post("/echo", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            try {
                assertNotNull(request.getHeaders().get("Host"));
                JSONObject json = new JSONObject();
                if (request.getBody() instanceof UrlEncodedFormBody) {
                    UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                } else if (request.getBody() instanceof JSONObjectBody) {
                    json = ((JSONObjectBody) request.getBody()).get();
                } else if (request.getBody() instanceof StringBody) {
                    json.put("foo", ((StringBody) request.getBody()).get());
                } else if (request.getBody() instanceof MultipartFormDataBody) {
                    MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                }
                response.send(json);
            } catch (Exception e) {
            }
        }
    });
}
Also used : NameValuePair(com.koushikdutta.async.http.NameValuePair) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) JSONObjectBody(com.koushikdutta.async.http.body.JSONObjectBody) JSONObject(org.json.JSONObject) StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) UrlEncodedFormBody(com.koushikdutta.async.http.body.UrlEncodedFormBody) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 4 with StringBody

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

the class XHRPollingTransport method postMessage.

private void postMessage(String message) {
    if (!message.startsWith("5"))
        return;
    AsyncHttpRequest request = new AsyncHttpPost(computedRequestUrl());
    request.setBody(new StringBody(message));
    client.executeString(request, null);
}
Also used : StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Aggregations

StringBody (com.koushikdutta.async.http.body.StringBody)4 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)3 AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)2 JSONObject (org.json.JSONObject)2 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)1 NameValuePair (com.koushikdutta.async.http.NameValuePair)1 JSONObjectBody (com.koushikdutta.async.http.body.JSONObjectBody)1 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)1 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1