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

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

Introduction

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

Prototype

public abstract String getResponseText();

Source Link

Document

Gets the response text.

Usage

From source file:jake2.gwt.client.GwtResourceLoaderImpl.java

License:Open Source License

public void loadResourceAsync(final String path, final ResourceLoader.Callback callback) {
    XMLHttpRequest req = XMLHttpRequest.create();

    final String eol = path.endsWith(".bsp") ? "\n" : "\r";

    final Exception e = new Exception();
    final int mySequenceNumber = freeSequenceNumber++;

    req.setOnReadyStateChange(new ReadyStateChangeHandler() {
        boolean receivingMsg;

        public void onReadyStateChange(final XMLHttpRequest xhr) {
            if (xhr.getReadyState() == 3 && !receivingMsg) {
                Com.Printf("Receiving #" + mySequenceNumber + ": " + path + eol);
                receivingMsg = true;//w  ww.  j av  a  2 s  . co  m
            } else if (xhr.getReadyState() == 4) {
                if (mySequenceNumber < ignoreSequenceNumbersBelow) {
                    Com.Printf("Ignoring outdated response #" + mySequenceNumber + ": " + path + "\n");
                } else {
                    String response;
                    if (xhr.getStatus() != 200) {
                        Com.Printf("Failed to load file #" + currentSequenceNumber + "\n");
                        Compatibility.printStackTrace(e);
                        ResourceLoader.fail(new IOException("status = " + xhr.getStatus()));
                        response = null;
                    } else {
                        response = xhr.getResponseText();
                        Com.Printf("Received response #" + mySequenceNumber + ": " + path + "\r");
                    }
                    readyList.add(0, new ResponseHandler(mySequenceNumber, callback, response));
                    if (mySequenceNumber == currentSequenceNumber) {
                        processReadyList();
                    }
                }
            }
        }
    });

    Com.Printf("Requesting: " + path + eol);

    overrideMimeType(req, "text/plain; charset=x-user-defined");
    req.open("GET", "/baseq2/" + path);
    req.send();
}

From source file:net.zschech.gwt.comet.client.impl.HTTPRequestCometTransport.java

License:Apache License

@Override
public void connect(int connectionCount) {
    super.connect(connectionCount);
    read = 0;/*from w  ww  . j a va 2 s  .co  m*/

    xmlHttpRequest = XMLHttpRequest.create();
    try {
        xmlHttpRequest.open("GET", getUrl(connectionCount));
        xmlHttpRequest.setRequestHeader("Accept", "application/comet");
        xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() {
            @Override
            public void onReadyStateChange(XMLHttpRequest request) {
                if (!disconnecting) {
                    switch (request.getReadyState()) {
                    case XMLHttpRequest.LOADING:
                        onReceiving(request.getStatus(), request.getResponseText());
                        break;
                    case XMLHttpRequest.DONE:
                        onLoaded(request.getStatus(), request.getResponseText());
                        break;
                    }
                }
            }
        });
        xmlHttpRequest.send();
    } catch (JavaScriptException e) {
        xmlHttpRequest = null;
        listener.onError(new RequestException(e.getMessage()), false);
    }
}

From source file:org.atmosphere.gwt.client.impl.HTTPRequestCometTransport.java

License:Apache License

@Override
public void connect(int connectionCount) {
    init();/*from   www  .  j  av  a  2  s  .c  o m*/
    xmlHttpRequest = XMLHttpRequest.create();
    try {
        xmlHttpRequest.open("GET", getUrl(connectionCount));
        xmlHttpRequest.setRequestHeader("Accept", "application/comet");
        xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() {
            @Override
            public void onReadyStateChange(XMLHttpRequest request) {
                if (!aborted) {
                    switch (request.getReadyState()) {
                    case XMLHttpRequest.LOADING:
                        onReceiving(request.getStatus(), request.getResponseText());
                        if (needPolling()) {
                            pollingTimer.scheduleRepeating(POLLING_INTERVAL);
                        }
                        break;
                    case XMLHttpRequest.DONE:
                        onLoaded(request.getStatus(), request.getResponseText());
                        pollingTimer.cancel();
                        break;
                    }
                } else {
                    request.clearOnReadyStateChange();
                    if (request.getReadyState() != XMLHttpRequest.DONE) {
                        request.abort();
                    }
                }
            }
        });
        xmlHttpRequest.send();
    } catch (JavaScriptException e) {
        if (xmlHttpRequest != null) {
            xmlHttpRequest.abort();
            xmlHttpRequest = null;
        }
        listener.onError(new RequestException(e.getMessage()), false);
    }
}

From source file:org.parallax3d.parallax.platforms.gwt.preloader.AssetDownloader.java

License:Apache License

public void loadText(String url, final FileListener<String> listener) {
    XMLHttpRequest request = XMLHttpRequest.create();
    request.setOnReadyStateChange(new ReadyStateChangeHandler() {
        @Override/* ww  w .j  av a2s  . c om*/
        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();
}

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/*w w  w  .  jav  a2  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 {//w  w  w . j  av  a2s  .c  o  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  www . ja va2s .  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 .jav 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.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);/*from w  w  w  .  j  ava 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());
            }
        }
    });
}