Example usage for com.google.gwt.xhr.client XMLHttpRequest getReadyState

List of usage examples for com.google.gwt.xhr.client XMLHttpRequest getReadyState

Introduction

In this page you can find the example usage for com.google.gwt.xhr.client XMLHttpRequest getReadyState.

Prototype

public abstract int getReadyState();

Source Link

Document

Get's the current ready-state

Usage

From source file:playn.html.HtmlAssetManager.java

License:Apache License

private void doXhr(final String fullPath, final ResourceCallback<String> callback) {
    XMLHttpRequest xhr = XMLHttpRequest.create();
    xhr.setOnReadyStateChange(new ReadyStateChangeHandler() {
        @Override//from www .java 2 s. c  o  m
        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.error(
                            new RuntimeException("Error getting " + fullPath + " : " + xhr.getStatusText()));
                } else {
                    if (LOG_XHR_SUCCESS) {
                        PlayN.log().debug("xhr::onReadyStateChange[" + fullPath + "](readyState = " + readyState
                                + "; status = " + status + ")");
                    }
                    // TODO(fredsa): Remove try-catch and materialized exception once issue 6562 is fixed
                    // http://code.google.com/p/google-web-toolkit/issues/detail?id=6562
                    try {
                        callback.done(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();
}

From source file:playn.html.HtmlNet.java

License:Apache License

public void get(String url, final Callback<String> callback) {
    try {/*from   w  w w  .j a v  a  2 s.co  m*/
        XMLHttpRequest xhr = XMLHttpRequest.create();
        xhr.open("GET", url);
        xhr.setOnReadyStateChange(new ReadyStateChangeHandler() {
            @Override
            public void onReadyStateChange(XMLHttpRequest xhr) {
                if (xhr.getReadyState() == XMLHttpRequest.DONE) {
                    if (xhr.getStatus() >= 400) {
                        callback.onFailure(new RuntimeException("Bad HTTP status code: " + xhr.getStatus()));
                    } else {
                        callback.onSuccess(xhr.getResponseText());
                    }
                }
            }
        });
        xhr.send();
    } catch (Exception e) {
        callback.onFailure(e);
    }
}

From source file:playn.html.HtmlNet.java

License:Apache License

@Override
public void post(String url, String data, final Callback<String> callback) {
    try {//from w w w.j a  v  a 2s .  c om
        XMLHttpRequest xhr = XMLHttpRequest.create();
        xhr.open("POST", url);
        xhr.setOnReadyStateChange(new ReadyStateChangeHandler() {
            @Override
            public void onReadyStateChange(XMLHttpRequest xhr) {
                if (xhr.getReadyState() == XMLHttpRequest.DONE) {
                    if (xhr.getStatus() >= 400) {
                        callback.onFailure(new RuntimeException("Bad HTTP status code: " + xhr.getStatus()));
                    } else {
                        callback.onSuccess(xhr.getResponseText());
                    }
                }
            }
        });
        xhr.send(data);
    } catch (Exception e) {
        callback.onFailure(e);
    }
}

From source file:playn.http.HttpHtml.java

License:Apache License

public void invokeHttpMethod(final HttpRequest request, final Callback<HttpResponse> callback) {
    try {/*from  w  w  w . j  a v a  2  s.  co m*/
        XMLHttpRequest xhr = XMLHttpRequest.create();
        xhr.open(request.getMethod().toString(), request.getUrl());
        for (Map.Entry<String, String> header : request.getHeaders()) {
            xhr.setRequestHeader(header.getKey(), header.getValue());
        }
        xhr.setOnReadyStateChange(new ReadyStateChangeHandler() {
            @Override
            public void onReadyStateChange(XMLHttpRequest xhr) {
                if (xhr.getReadyState() == XMLHttpRequest.DONE) {
                    try {
                        gotResponse(request, xhr, xhr.getResponseText(), callback);
                    } catch (IOException e) {
                        callback.onFailure(e);
                    }
                }
            }
        });
        xhr.send(request.getBody());
    } catch (Exception e) {
        callback.onFailure(e);
    }
}

From source file:thothbot.parallax.core.client.textures.CompressedTexture.java

License:Open Source License

public CompressedTexture(final String url, final ImageLoadHandler imageLoadHandler) {

    this.mipmaps = new ArrayList<DataTexture>();

    XMLHttpRequest binxhr = (XMLHttpRequest) XMLHttpRequest.create();
    binxhr.open("GET", url);
    binxhr.setResponseType("arraybuffer");
    binxhr.send();//w ww.  ja  va 2 s .co  m

    binxhr.setOnReadyStateChange(new ReadyStateChangeHandler() {
        @Override
        public void onReadyStateChange(XMLHttpRequest xhr) {
            XMLHttpRequest binxhr = (XMLHttpRequest) xhr;
            if (binxhr.getReadyState() == XMLHttpRequest.DONE) {
                binxhr.clearOnReadyStateChange();
                ArrayBuffer buffer = binxhr.getResponseArrayBuffer();
                parseDDS(buffer, true);

                setGenerateMipmaps(false);
                setNeedsUpdate(true);

                if (imageLoadHandler != null)
                    imageLoadHandler.onImageLoad(CompressedTexture.this);
            }
        }
    });
}

From source file:thothbot.parallax.loader.shared.XHRLoader.java

License:Open Source License

protected void load() {
    final XMLHttpRequest request = XMLHttpRequest.create();
    request.open("GET", url);
    request.setResponseType(this.responseType);
    request.send(null);//  w w  w.j  a  v  a 2s  .c  o  m

    loadHandlers.add(modelLoadHandler);

    if (XHRLoader.loaderProgressHandler != null)
        XHRLoader.loaderProgressHandler.onProgressUpdate(loadHandlers.size());

    request.setOnReadyStateChange(new ReadyStateChangeHandler() {

        @Override
        public void onReadyStateChange(XMLHttpRequest xhr) {
            if (xhr.getReadyState() == XMLHttpRequest.DONE) {
                if (xhr.getStatus() >= 400) {
                    Log.error("Error while loading file: " + url + ", status: " + xhr.getStatus());
                } else {
                    AbstractGeometry geometry = null;

                    if (xhr.getResponseType().equals("arraybuffer")) {
                        com.google.gwt.typedarrays.shared.ArrayBuffer origin = xhr.getResponseArrayBuffer();

                        geometry = parse(ArrayBuffer.copy(origin));
                    } else
                        geometry = parse(xhr.getResponseText());

                    modelLoadHandler.onModelLoaded(XHRLoader.this, geometry);
                }

                request.clearOnReadyStateChange();
                request.abort();

                loadHandlers.remove(modelLoadHandler);
                if (XHRLoader.loaderProgressHandler != null)
                    XHRLoader.loaderProgressHandler.onProgressUpdate(loadHandlers.size());
            }
        }
    });
}