com.google.gwt.xhr.client.XMLHttpRequest类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(104)

本文整理了Java中com.google.gwt.xhr.client.XMLHttpRequest类的一些代码示例,展示了XMLHttpRequest类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XMLHttpRequest类的具体详情如下:
包路径:com.google.gwt.xhr.client.XMLHttpRequest
类名称:XMLHttpRequest

XMLHttpRequest介绍

[英]The native XMLHttpRequest object. Most applications should use the higher- level com.google.gwt.http.client.RequestBuilder class unless they need specific functionality provided by the XMLHttpRequest object. See http://www.w3.org/TR/XMLHttpRequest//
[中]

代码示例

代码示例来源:origin: libgdx/libgdx

@Override
  public void onReadyStateChange (XMLHttpRequest xhr) {
    if (xhr.getReadyState() == XMLHttpRequest.DONE) {
      if (xhr.getStatus() != 200) {
        listener.onFailure();
      } else {
        listener.onSuccess(xhr.getResponseText());
      }
    }
  }
});

代码示例来源:origin: libgdx/libgdx

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();
}

代码示例来源:origin: libgdx/libgdx

@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));
      }
    }
  }
});

代码示例来源:origin: sriharshachilakapati/SilenceEngine

@Override
  public void readTextFile(FilePath file, UniCallback<String> onComplete, UniCallback<Throwable> onError)
  {
    // Create a XMLHttpRequest to load the file into a direct buffer
    XMLHttpRequest request = XMLHttpRequest.create();
    request.open("GET", file.getAbsolutePath());

    // Set to read as default mode and attach a handler
    request.setResponseType(XMLHttpRequest.ResponseType.Default);
    request.setOnReadyStateChange(xhr ->
    {
      if (request.getReadyState() == XMLHttpRequest.DONE)
      {
        if (request.getStatus() == 200)
          // Invoke the onComplete handler
          onComplete.invoke(request.getResponseText());
        else
          onError.invoke(new SilenceException("Error fetching the file: " + request.getStatusText()));
      }
    });

    // Send the request
    request.send();
  }
}

代码示例来源:origin: sriharshachilakapati/SilenceEngine

@Override
public Promise<Boolean> exists()
{
  return new Promise<>((resolve, reject) ->
  {
    XMLHttpRequest request = XMLHttpRequest.create();
    request.open("HEAD", getAbsolutePath());
    request.setOnReadyStateChange(xhr ->
    {
      if (request.getStatus() == 404)
        resolve.invoke(false);
      else if (request.getReadyState() == XMLHttpRequest.DONE && request.getStatus() == 200)
        resolve.invoke(true);
    });
    request.send();
  });
}

代码示例来源:origin: com.vaadin.external.atmosphere/atmosphere-gwt-client

@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();
      }
    }
  }
});

代码示例来源:origin: org.atmosphere/atmosphere-gwt-client

@Override
  public void run() {
    r.clearOnReadyStateChange();
    if (r.getReadyState() != XMLHttpRequest.DONE
        && r.getReadyState() != XMLHttpRequest.UNSENT) {
      listener.onDisconnected();
      r.abort();
    }
    r = null;
  }
}.schedule(5000);

代码示例来源:origin: com.google.gwt/gwt-servlet

public void onReadyStateChange(XMLHttpRequest xhr) {
  if (xhr.getReadyState() == XMLHttpRequest.DONE) {
   xhr.clearOnReadyStateChange();
   request.fireOnResponseReceived(callback);
  }
 }
});

代码示例来源:origin: stackoverflow.com

void postJson(Map<String, String> map, String url) {
  XMLHttpRequest request = new XMLHttpRequest();

  // Post asynchronously
  request.open("POST", url, true);
  request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

  // Encode the data as JSON before sending
  String mapAsJson = JSON.stringify(toJsObject(map));
  request.send(mapAsJson);
}

jsweet.lang.Object toJsObject(Map<String, String> map) {
  jsweet.lang.Object jsObject = new jsweet.lang.Object();

  // Put the keys and values from the map into the object
  for (Entry<String, String> keyVal : map.entrySet()) {
    jsObject.$set(keyVal.getKey(), keyVal.getValue());
  }
  return jsObject;
}

代码示例来源:origin: threerings/playn

@Override
public void onReadyStateChange(XMLHttpRequest xhr) {
 int readyState = xhr.getReadyState();
 if (readyState == XMLHttpRequest.DONE) {
  int status = xhr.getStatus();
       + "; status = " + status + ")");
   callback.onFailure(
    new RuntimeException("Error getting " + fullPath + " : " + xhr.getStatusText()));
  } else {
   if (LOG_XHR_SUCCESS) {
    callback.onSuccess(xhr.getResponseText());
   } catch(JavaScriptException e) {
    if (GWT.isProdMode()) {

代码示例来源:origin: io.playn/playn-html

@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)) {
    plat.log().error("xhr::onReadyStateChange[" + path + "]" +
             "(readyState = " + readyState + "; status = " + status + ")");
    result.fail(new Exception("Error getting " + path + " : " + xhr.getStatusText()));
   } else {
    if (LOG_XHR_SUCCESS) plat.log().debug("xhr::onReadyStateChange[" + path + "]" +
                       "(readyState = " + readyState +
                       "; status = " + status + ")");
    result.succeed(xhr);
   }
  }
 }
});

代码示例来源:origin: com.google.gwt/gwt-servlet

protected boolean isResponseReady() {
  return xmlHttpRequest.getReadyState() == XMLHttpRequest.DONE;
 }
}

代码示例来源:origin: stackoverflow.com

XMLHttpRequest req = XMLHttpRequest.create();
req.open("HEAD", fileURL);
req.setOnReadyStateChange(new ReadyStateChangeHandler() {

  @Override
  public void onReadyStateChange(XMLHttpRequest xhr) {

    if (xhr.getReadyState() == XMLHttpRequest.DONE) {
      if (xhr.getStatus() == 200) {
        Window.open(fileURL, winTitle, "");
      } else {
        // TODO handle other status codes
      }
    }
  }
});
req.send();

代码示例来源:origin: org.atmosphere/atmosphere-gwt-client

@Override
  public void run() {
    if (xmlHttpRequest != null && read < xmlHttpRequest.getResponseText().length()) {
      onReceiving(xmlHttpRequest.getStatus(), xmlHttpRequest.getResponseText());
    }
  }
};

代码示例来源:origin: com.google.gwt/gwt-servlet

/**
 * Cancels a pending request. If the request has already been canceled or if
 * it has timed out no action is taken.
 */
public void cancel() {
 if (xmlHttpRequest == null) {
  return;
 }
 timer.cancel();
 /*
  * There is a strange race condition that occurs on Mozilla when you cancel
  * a request while the response is coming in. It appears that in some cases
  * the onreadystatechange handler is still called after the handler function
  * has been deleted and during the call to XmlHttpRequest.abort(). So we
  * null the xmlHttpRequest here and that will prevent the
  * fireOnResponseReceived method from calling the callback function.
  * 
  * Setting the onreadystatechange handler to null gives us the correct
  * behavior in Mozilla but crashes IE. That is why we have chosen to fixed
  * this in Java by nulling out our reference to the XmlHttpRequest object.
  */
 final XMLHttpRequest xhr = xmlHttpRequest;
 xmlHttpRequest = null;
 xhr.clearOnReadyStateChange();
 xhr.abort();
}

代码示例来源:origin: com.google.gwt/gwt-servlet

@Override
public String getText() {
 return xmlHttpRequest.getResponseText();
}

代码示例来源:origin: com.google.gwt/gwt-servlet

/**
 * Initiates a request with no request data. This simply calls
 * {@link #send(String)} with <code>null</code> as an argument, because the
 * no-argument <code>send()</code> method is unavailable on Firefox.
 */
public final void send() {
 send(null);
}

代码示例来源:origin: com.google.gwt/gwt-servlet

@Override
public int getStatusCode() {
 return xmlHttpRequest.getStatus();
}

代码示例来源:origin: com.google.gwt/gwt-servlet

private void setHeaders(XMLHttpRequest xmlHttpRequest)
   throws RequestException {
  if (headers != null && headers.size() > 0) {
   for (Map.Entry<String, String> header : headers.entrySet()) {
    try {
     xmlHttpRequest.setRequestHeader(header.getKey(), header.getValue());
    } catch (JavaScriptException e) {
     throw new RequestException(e.getMessage());
    }
   }
  } else {
   xmlHttpRequest.setRequestHeader("Content-Type",
     "text/plain; charset=utf-8");
  }
 }
}

代码示例来源:origin: com.google.gwt/gwt-servlet

/**
 * Sets the response type.
 * <p>
 * See <a href="http://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute"
 * >http://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute</a>
 *
 * @param responseType the type of response desired.  See {@link ResponseType}
 *     for limitations on using the different values
 */
public final void setResponseType(ResponseType responseType) {
 this.setResponseType(responseType.getResponseTypeString());
}

相关文章