com.koushikdutta.async.http.body.JSONArrayBody Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of androidasync Show documentation
Show all versions of androidasync Show documentation
Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
package com.koushikdutta.async.http.body;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.DataSink;
import com.koushikdutta.async.Util;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.parser.JSONArrayParser;
import org.json.JSONArray;
public class JSONArrayBody implements AsyncHttpRequestBody {
public JSONArrayBody() {
}
byte[] mBodyBytes;
JSONArray json;
public JSONArrayBody(JSONArray json) {
this();
this.json = json;
}
@Override
public void parse(DataEmitter emitter, final CompletedCallback completed) {
new JSONArrayParser().parse(emitter).setCallback(new FutureCallback() {
@Override
public void onCompleted(Exception e, JSONArray result) {
json = result;
completed.onCompleted(e);
}
});
}
@Override
public void write(AsyncHttpRequest request, DataSink sink, final CompletedCallback completed) {
Util.writeAll(sink, mBodyBytes, completed);
}
@Override
public String getContentType() {
return "application/json";
}
@Override
public boolean readFullyOnRequest() {
return true;
}
@Override
public int length() {
mBodyBytes = json.toString().getBytes();
return mBodyBytes.length;
}
public static final String CONTENT_TYPE = "application/json";
@Override
public JSONArray get() {
return json;
}
}