use of com.koushikdutta.async.http.body.Part in project ion by koush.
the class HttpTests method testMultipartFileContentType.
public void testMultipartFileContentType() throws Exception {
File f = getContext().getFileStreamPath("empty");
f.getParentFile().mkdirs();
f.createNewFile();
AsyncHttpServer httpServer = new AsyncHttpServer();
httpServer.post("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
body.setMultipartCallback(new MultipartFormDataBody.MultipartCallback() {
@Override
public void onPart(Part part) {
response.send(part.getContentType());
}
});
}
});
try {
httpServer.listen(AsyncServer.getDefault(), 6666);
String mime = Ion.with(getContext()).load("http://localhost:6666/").setMultipartFile("foo", "test/mime", f).asString().get(1000, TimeUnit.MILLISECONDS);
assertEquals(mime, "test/mime");
} finally {
httpServer.stop();
}
}
use of com.koushikdutta.async.http.body.Part in project ion by koush.
the class ProgressTests method testUpload.
public void testUpload() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.listen(Ion.getDefault(getContext()).getServer(), 5000);
httpServer.post("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
MultipartFormDataBody multipartFormDataBody = (MultipartFormDataBody) request.getBody();
multipartFormDataBody.setMultipartCallback(new MultipartFormDataBody.MultipartCallback() {
@Override
public void onPart(Part part) {
}
});
multipartFormDataBody.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
response.send("Got parts!");
}
});
}
});
final Semaphore semaphore = new Semaphore(0);
Ion.with(getContext()).load("http://localhost:5000/").uploadProgress(new ProgressCallback() {
@Override
public void onProgress(long downloaded, long total) {
semaphore.release();
}
}).setMultipartParameter("foo", "bar").asString().get();
assertTrue(semaphore.tryAcquire());
} finally {
Ion.getDefault(getContext()).getServer().stop();
}
}
use of com.koushikdutta.async.http.body.Part in project AndroidAsync by koush.
the class MultipartTests 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.post("/", new HttpServerRequestCallback() {
int gotten = 0;
@Override
public void onRequest(final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
final MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
body.setMultipartCallback(new MultipartCallback() {
@Override
public void onPart(Part part) {
if (part.isFile()) {
body.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
gotten += bb.remaining();
bb.recycle();
}
});
}
}
});
request.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
response.send(body.getField("baz") + gotten + body.getField("foo"));
}
});
}
});
}
use of com.koushikdutta.async.http.body.Part in project ion by koush.
the class IonRequestBuilder method addMultipartParts.
@Override
public Builders.Any.M addMultipartParts(Part... parameters) {
if (multipartBody == null) {
multipartBody = new MultipartFormDataBody();
setBody(multipartBody);
}
for (Part part : parameters) {
multipartBody.addPart(part);
}
return this;
}
use of com.koushikdutta.async.http.body.Part in project ion by koush.
the class IonRequestBuilder method addMultipartParts.
@Override
public IonRequestBuilder addMultipartParts(Iterable<Part> parameters) {
if (multipartBody == null) {
multipartBody = new MultipartFormDataBody();
setBody(multipartBody);
}
for (Part part : parameters) {
multipartBody.addPart(part);
}
return this;
}
Aggregations