use of com.koushikdutta.async.http.NameValuePair 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.NameValuePair in project AndroidAsync by koush.
the class MainActivity method getChartFile.
private void getChartFile() {
final ImageView iv = chart;
final String filename = getFileStreamPath(randomFile()).getAbsolutePath();
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("cht", "lc"));
pairs.add(new BasicNameValuePair("chtt", "This is a google chart"));
pairs.add(new BasicNameValuePair("chs", "512x512"));
pairs.add(new BasicNameValuePair("chxt", "x"));
pairs.add(new BasicNameValuePair("chd", "t:40,20,50,20,100"));
UrlEncodedFormBody writer = new UrlEncodedFormBody(pairs);
try {
AsyncHttpPost post = new AsyncHttpPost("http://chart.googleapis.com/chart");
post.setBody(writer);
AsyncHttpClient.getDefaultInstance().executeFile(post, filename, new AsyncHttpClient.FileCallback() {
@Override
public void onCompleted(Exception e, AsyncHttpResponse response, File result) {
if (e != null) {
e.printStackTrace();
return;
}
Bitmap bitmap = BitmapFactory.decodeFile(filename);
result.delete();
if (bitmap == null)
return;
BitmapDrawable bd = new BitmapDrawable(bitmap);
assignImageView(iv, bd);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of com.koushikdutta.async.http.NameValuePair in project AndroidAsync by koush.
the class UrlEncodedFormBody method buildData.
private void buildData() {
boolean first = true;
StringBuilder b = new StringBuilder();
try {
for (NameValuePair pair : mParameters) {
if (pair.getValue() == null)
continue;
if (!first)
b.append('&');
first = false;
b.append(URLEncoder.encode(pair.getName(), "UTF-8"));
b.append('=');
b.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
mBodyBytes = b.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
Aggregations