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