use of com.google.gwt.xhr.client.ReadyStateChangeHandler in project libgdx by libgdx.
the class AssetDownloader method loadText.
public void loadText(String url, final AssetLoaderListener<String> listener) {
XMLHttpRequest request = XMLHttpRequest.create();
request.setOnReadyStateChange(new ReadyStateChangeHandler() {
@Override
public void onReadyStateChange(XMLHttpRequest xhr) {
if (xhr.getReadyState() == XMLHttpRequest.DONE) {
if (xhr.getStatus() != 200) {
listener.onFailure();
} else {
listener.onSuccess(xhr.getResponseText());
}
}
}
});
setOnProgress(request, listener);
request.open("GET", url);
request.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
request.send();
}
use of com.google.gwt.xhr.client.ReadyStateChangeHandler in project playn by threerings.
the class HtmlAssets method doXhr.
private void doXhr(final String fullPath, final Callback<String> callback) {
XMLHttpRequest xhr = XMLHttpRequest.create();
xhr.setOnReadyStateChange(new ReadyStateChangeHandler() {
@Override
public void onReadyStateChange(XMLHttpRequest xhr) {
int readyState = xhr.getReadyState();
if (readyState == XMLHttpRequest.DONE) {
int status = xhr.getStatus();
// status code 0 will be returned for non-http requests, e.g. file://
if (status != 0 && (status < 200 || status >= 400)) {
PlayN.log().error("xhr::onReadyStateChange[" + fullPath + "](readyState = " + readyState + "; status = " + status + ")");
callback.onFailure(new RuntimeException("Error getting " + fullPath + " : " + xhr.getStatusText()));
} else {
if (LOG_XHR_SUCCESS) {
PlayN.log().debug("xhr::onReadyStateChange[" + fullPath + "](readyState = " + readyState + "; status = " + status + ")");
}
// http://code.google.com/p/google-web-toolkit/issues/detail?id=6562
try {
callback.onSuccess(xhr.getResponseText());
} catch (JavaScriptException e) {
if (GWT.isProdMode()) {
throw e;
} else {
JavaScriptException materialized = new JavaScriptException(e.getName(), e.getDescription());
materialized.setStackTrace(e.getStackTrace());
throw materialized;
}
}
}
}
}
});
if (LOG_XHR_SUCCESS) {
PlayN.log().debug("xhr.open('GET', '" + fullPath + "')...");
}
xhr.open("GET", fullPath);
if (LOG_XHR_SUCCESS) {
PlayN.log().debug("xhr.send()...");
}
xhr.send();
}
use of com.google.gwt.xhr.client.ReadyStateChangeHandler in project libgdx by libgdx.
the class AssetDownloader method loadBinary.
public void loadBinary(final String url, final AssetLoaderListener<Blob> listener) {
XMLHttpRequest request = XMLHttpRequest.create();
request.setOnReadyStateChange(new ReadyStateChangeHandler() {
@Override
public void onReadyStateChange(XMLHttpRequest xhr) {
if (xhr.getReadyState() == XMLHttpRequest.DONE) {
if (xhr.getStatus() != 200) {
listener.onFailure();
} else {
Int8Array data = TypedArrays.createInt8Array(xhr.getResponseArrayBuffer());
listener.onSuccess(new Blob(data));
}
}
}
});
setOnProgress(request, listener);
request.open("GET", url);
request.setResponseType(ResponseType.ArrayBuffer);
request.send();
}
use of com.google.gwt.xhr.client.ReadyStateChangeHandler in project playn by threerings.
the class HtmlNet method execute.
@Override
protected void execute(final BuilderImpl req, final Callback<Response> callback) {
try {
XMLHttpRequest xhr = XMLHttpRequest.create();
xhr.open(req.method(), req.url);
for (Header header : req.headers) {
xhr.setRequestHeader(header.name, header.value);
}
xhr.setOnReadyStateChange(new ReadyStateChangeHandler() {
@Override
public void onReadyStateChange(final XMLHttpRequest xhr) {
if (xhr.getReadyState() == XMLHttpRequest.DONE) {
callback.onSuccess(new StringResponse(xhr.getStatus(), xhr.getResponseText()) {
@Override
protected Map<String, List<String>> extractHeaders() {
Map<String, List<String>> headers = new HashMap<String, List<String>>();
String block = xhr.getAllResponseHeaders();
for (String line : block.split("\r\n")) {
int cidx = line.indexOf(":");
if (cidx > 0) {
String name = line.substring(0, cidx);
List<String> values = headers.get(name);
if (values == null)
headers.put(name, values = new ArrayList<String>());
values.add(line.substring(cidx + 1).trim());
}
}
return headers;
}
@Override
public String header(String name) {
// in those cases; yay web!
return xhr.getResponseHeader(name);
}
@Override
public List<String> headers(String name) {
// if we were able to parse the headers ourselves, use those, but if not (due
// perhaps to bugs, etc.) then fall back to using getResponseHeader
List<String> values = super.headers(name);
if (!values.isEmpty())
return values;
String value = xhr.getResponseHeader(name);
return (value == null) ? values : Collections.singletonList(value);
}
});
}
}
});
if (req.isPost()) {
if (req.payloadBytes != null) {
throw new UnsupportedOperationException("Raw bytes not currently supported in HTML5.");
}
xhr.setRequestHeader("Content-Type", req.contentType());
xhr.send(req.payloadString);
} else {
xhr.send();
}
} catch (Exception e) {
callback.onFailure(e);
}
}
Aggregations