Search in sources :

Example 1 with JSONObjectBody

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

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

Example 3 with JSONObjectBody

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

the class HttpServerTests method testJSONObject.

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

Example 4 with JSONObjectBody

use of com.koushikdutta.async.http.body.JSONObjectBody in project simperium-android by Simperium.

the class AsyncAuthClient method buildRequest.

public AsyncHttpPost buildRequest(String path, JSONObject body) {
    Uri uri = buildUri(path);
    AsyncHttpPost request = new AsyncHttpPost(uri);
    request.setBody(new JSONObjectBody(body));
    request.setHeader(API_KEY_HEADER_NAME, mSecret);
    return request;
}
Also used : JSONObjectBody(com.koushikdutta.async.http.body.JSONObjectBody) Uri(android.net.Uri) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost)

Aggregations

JSONObjectBody (com.koushikdutta.async.http.body.JSONObjectBody)4 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)3 JSONObject (org.json.JSONObject)3 Uri (android.net.Uri)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 NameValuePair (com.koushikdutta.async.http.NameValuePair)1 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)1 StringBody (com.koushikdutta.async.http.body.StringBody)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