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) {
}
}
});
}
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"));
}
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");
}
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;
}
Aggregations