Java Code Examples for com.google.gwt.xhr.client.XMLHttpRequest#setOnReadyStateChange()

The following examples show how to use com.google.gwt.xhr.client.XMLHttpRequest#setOnReadyStateChange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 6 votes vote down vote up
public static <I, O> void ajax(String protocol, String url, I model, ObjectMapper<I> inMapper,
		ObjectMapper<O> outMapper, BiConsumer<Integer, O> handler) {
	XMLHttpRequest xhr = XMLHttpRequest.create();
	xhr.setOnReadyStateChange(a -> {
		if (handler == null || xhr.getReadyState() != 4) {
			return;
		}
		O result = null;
		if (xhr.getStatus() == 200) {
			result = out(xhr.getResponseText(), outMapper);
		}
		handler.accept(xhr.getStatus(), result);
	});
	xhr.open(protocol, url);
	xhr.send(in(model, inMapper));
}
 
Example 2
Source File: Xhr.java    From flow with Apache License 2.0 6 votes vote down vote up
private static XMLHttpRequest request(XMLHttpRequest xhr, String method,
        String url, String requestData, String contentType,
        Callback callback) {
    try {
        xhr.setOnReadyStateChange(new Handler(callback));
        xhr.open(method, url);
        xhr.setRequestHeader("Content-type", contentType);
        xhr.setWithCredentials(true);
        xhr.send(requestData);
    } catch (JavaScriptException e) {
        // Just fail.
        Console.error(e);
        callback.onFail(xhr, e);
        xhr.clearOnReadyStateChange();
    }
    return xhr;
}
 
Example 3
Source File: Client.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
private void clicked(Event e) {
	button.setAttribute("disabled", "");
	thinking.getStyle().setProperty("display", "");

	XMLHttpRequest xhr = XMLHttpRequest.create();
	xhr.setOnReadyStateChange(a -> {
		if (xhr.getReadyState() == 4 && xhr.getStatus() == 200) {
			responsed(xhr.getResponseText());
		}
	});
	xhr.open("POST", url);
	xhr.send();
}
 
Example 4
Source File: Xhr.java    From flow with Apache License 2.0 5 votes vote down vote up
private static XMLHttpRequest request(XMLHttpRequest xhr, String method,
        String url, final Callback callback) {
    try {
        xhr.setOnReadyStateChange(new Handler(callback));
        xhr.open(method, url);
        xhr.send();
    } catch (JavaScriptException e) {
        // Just fail.
        Console.error(e);
        callback.onFail(xhr, e);
        xhr.clearOnReadyStateChange();
    }
    return xhr;
}
 
Example 5
Source File: PageDecoder.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
private void downloadFile(final String url) {
	XMLHttpRequest request = XMLHttpRequest.create();
	request.open("GET", url);
	request.setResponseType(ResponseType.ArrayBuffer);
	request.setOnReadyStateChange(new ReadyStateChangeHandler() {
		@Override
		public void onReadyStateChange(XMLHttpRequest xhr) {
			if (xhr.getReadyState() == XMLHttpRequest.DONE) {
				downloadsInProgress--;
				if (xhr.getStatus() == 200) {
					FileItem entry = getCachedFile(url);
					entry.data = TypedArrays.createUint8Array(xhr.getResponseArrayBuffer());
					entry.dataSize = entry.data.byteLength();
					filesMemoryUsage += entry.dataSize;
					checkFilesMemory();
					context.startProcessing();
					fireReady(url);
					continueDownload();
				} else {
					GWT.log("Error downloading " + url);
					GWT.log("response status: " + xhr.getStatus() + " " + xhr.getStatusText());
					context.setStatus(ProcessingContext.STATUS_ERROR);
					fileCache.get(url).downloadStarted = false;
				}
			}
		}
	});
	request.send();
	fileCache.get(url).downloadStarted = true;
	downloadsInProgress++;
}
 
Example 6
Source File: ImageCanvasTest.java    From TGAReader with MIT License 5 votes vote down vote up
private void addTGACanvas(String url) {
	XMLHttpRequest request = XMLHttpRequest.create();
	request.open("GET", url);
	request.setResponseType(ResponseType.ArrayBuffer);
	request.setOnReadyStateChange(new ReadyStateChangeHandler() {
		@Override
		public void onReadyStateChange(XMLHttpRequest xhr) {
			if(xhr.getReadyState() == XMLHttpRequest.DONE) {
				if(xhr.getStatus() >= 400) {
					// error
					System.out.println("Error");
				}
				else {
					try {
						ArrayBuffer arrayBuffer = xhr.getResponseArrayBuffer();
						Uint8ArrayNative u8array = Uint8ArrayNative.create(arrayBuffer);
						byte [] buffer = new byte[u8array.length()];
						for(int i=0; i<buffer.length; i++) {
							buffer[i] = (byte)u8array.get(i);
						}
						int pixels [] = TGAReader.read(buffer, TGAReader.ABGR);
						int width = TGAReader.getWidth(buffer);
						int height = TGAReader.getHeight(buffer);
						
						Canvas canvas = createImageCanvas(pixels, width, height);
						panel.add(canvas);
					}
					catch(Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	});
	request.send();
}
 
Example 7
Source File: TexturedCube.java    From TGAReader with MIT License 4 votes vote down vote up
void loadTexture(String url, int index) {
	final int i = index;
    XMLHttpRequest request = XMLHttpRequest.create();
    request.open("GET", url);
    request.setResponseType(ResponseType.ArrayBuffer);
    request.setOnReadyStateChange(new ReadyStateChangeHandler() {
        @Override
        public void onReadyStateChange(XMLHttpRequest xhr) {
            if(xhr.getReadyState() == XMLHttpRequest.DONE) {
                if(xhr.getStatus() >= 400) {
                    // error
                    System.out.println("Error");
                }
                else {
                	try {
	                	ArrayBuffer arrayBuffer = xhr.getResponseArrayBuffer();
	    				Uint8ArrayNative u8array = Uint8ArrayNative.create(arrayBuffer);
	    				byte [] buffer = new byte[u8array.length()];
	    				for(int i=0; i<buffer.length; i++) {
	    					buffer[i] = (byte)u8array.get(i);
	    				}
	    				
	    				int [] pixels = TGAReader.read(buffer, TGAReader.ABGR);
	    				int width = TGAReader.getWidth(buffer);
	    				int height = TGAReader.getHeight(buffer);
	    				
	    				Canvas canvas = createImageCanvas(pixels, width, height);
	    				
	    				WebGLTexture texture = gl.createTexture();
	    				gl.enable(TEXTURE_2D);
	    				gl.bindTexture(TEXTURE_2D, texture);
	    				
	    				gl.texImage2D(TEXTURE_2D, 0, RGBA, RGBA, UNSIGNED_BYTE, canvas.getElement());

	    				gl.texParameteri(TEXTURE_2D, TEXTURE_WRAP_S, CLAMP_TO_EDGE);
	    				gl.texParameteri(TEXTURE_2D, TEXTURE_WRAP_T, CLAMP_TO_EDGE);
	    				gl.texParameteri(TEXTURE_2D, TEXTURE_MAG_FILTER, LINEAR);
	    				gl.texParameteri(TEXTURE_2D, TEXTURE_MIN_FILTER, LINEAR);
	    				
	    				textures[i] = texture;
	    				draw();
                	}
                	catch(Exception e) {
                		e.printStackTrace();
                	}
                }
            }
        }
    });
    request.send();
}