XMLHttpRequest is an alternative for HTTP calls from GWT client side and allows the control over all aspects of requests/responses. But how to use it? javadoc address: http://www.gwtproject.org/javadoc/latest/com/google/gwt/xhr/client/class-use/XMLHttpRequest.html
How to use XMLHttpRequest in GWT?
2k Views Asked by hadiafifi At
1
There are 1 best solutions below
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in GWT
- Set log4j.properties for GWT
- Grid with rows in anchor not visible in GWT (UIBinder)
- PlayN and Firefox issues
- Simulate Key Combinations by scanning bar code
- Server Side Implementation in GWT
- gwt 2.0.0 in Eclipse Luna & gwt plugin version 3.8.0
- Disabiling 'X mark' in IE11 compatibility mode
- GWT: Site layout with frames
- GWT Datagrid slow scroll due to css rendering
- GWT Eclipse: using classes from other projects cause "Server class ... could not be found in the web app, but was found on the system classpath
- Cannot read property removeChild of null in jsni
- Debugging GWT app in production by loading SourceMaps from local environment
- GWT DataGrid column width issue after removing columns
- How run several instances of GWT application in IntelliJIDEA
- How to deal with large multi-module applications on GWT
Related Questions in GWT-2.8
- Debug Doesn’t Work After Migrating SmartGWT from v4.0 to v13.0
- Errai 4 running on Tomcat
- GWT Compiler : when is a compilation error fatal?
- Getting Uncaught RangeError: Maximum call stack size exceeded in GWT 2.9 post compilation
- GWT compilation error with Maven: No instance of Presenter is defined. Forget to annotate @Presenter or @EventHandler?
- Why can't GWT connect to superdevmode server in multi module maven project?
- Integrating Vaadin GWT Polymer Elements into an existing GWT project without usign Maven, Ant or Gradle
- How to use XMLHttpRequest in GWT?
- GWT RPC fails when launching app from different urls
- GWT compile error: Rebind result cannot be abstract DOMUtilImpl
- GWT - Error constructing Java AST
- Can't compile GWT app using 2.8.0-SNAPSHOT and Java 8 with gwt-maven-plugin
- GWT 2.8 websocket support
- Unresponsive tabs in Chrome and Safari after upgrading to GWT 2.8
- Eclipse / launch / Java Classpath enclosed in quotes / breaks 3rd party tools / blocks development
Related Questions in GWT-JSINTEROP
- How can i realize a Circuit Sandbox Client in a GWT app with IsInterop or Babel?
- How to call a JavaScript function from GWT with jsInterop?
- How to access window.opener while using gwt.jsinterop
- GWT com.google.gwt.json.client.* JSON classes not supporting for jsinterop?
- Controlling which classes GWT includes with -generateJsInteropExports
- Should I stop using GSS etc. if I want to be ready for GWT 3?
- How to cast an object to JsType?
- GWT - using compiled code (JsInterop) in a service worker
- GWT / JSInterop / Eclipse Plugin / Production Build
- How to expose JS patch function of Incremental DOM library in GWT app using @JsInterop
- How to import and use JsInterop with LibGDX (Gradle)?
- JsInterop - Get Value in Array
- How does Js.cast() perform its type checking?
- Problem efficiently mapping a native JavaScript library variable parameter API with Elemental2 / GWT?
- How to set a default value for a @JsProperty?
Related Questions in GWT-ELEMENTAL
- Exception in GWT Elemental Simple Example
- How does Js.cast() perform its type checking?
- Class Cast Exception when casting an elemental2.dom.Event to elemental2.dom.CustomEvent in GWT
- Problem efficiently mapping a native JavaScript library variable parameter API with Elemental2 / GWT?
- How to use XMLHttpRequest in GWT?
- How to get a reference to elemental.html.FormData
- GWT Websockets with Elemental
- Adding Elemental to GWT
- Reference/Documentation for GWT Elemental webRTC api
- Is there a way to get an input value in Java - elemental2?
- GWT - Error to load entry point
- Does GWT elemental use JsType or JSNI Overlays?
- Gwt elemental2: How can I convert between a gwt JavaScript object, and a JsInterop object?
- How to use the rear camera in a mobile navigator using Gwt-Elemental 2.8
- Is it possible to use the GWT Elemental collections in a non-elemental app?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You haven't mentioned what GWT version you use, so I assume the latest one. It means 2.8.2 or newer.
Elemental2 is the way to go
As it is mentioned in comments above,
Elemental2is the right way. I will explain it a bit.If you think about future-proof implementation (being aware of
GWT3/J2CLnew approach), please do not use legacy GWT stuff. It means please useelemental2.dom.XMLHttpRequestinstead ofcom.google.gwt.xhr.client.XMLHttpRequest(the one mentioned by you). Please do not usegwt-userdependency if possible, as it will be deprecated (if it is not already).The
Elemental2is an opensource project available here: https://github.com/google/elemental2. It is kind of a base library for the "new GWT". For easier migration of existing GWT2.x projects to GWT3.x, a part of the "old"gwt-useris currently being ported to the new approach usingJsInteroptechnique and mentionedElemental2. So definitelyElemental2is the way to go.Elemental2 and JsInterop in general
The specification is not yet that rich if about the new JsInterop approach, but at the moment you will find some introduction at least: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJsInterop.html
Examples
Please find an example for
XMLHttpRequestin this article: http://www.g-widgets.com/2016/09/09/gwt-http-requests-alternatives/If you look for examples, also a good way is to search this on the Github site this way: https://github.com/search?q=elemental2.dom.XMLHttpRequest&type=Code.
(To use Github search you need to be logged in, in other case you will see "Whoa there! You have triggered an abuse blah blah..." )
One of the results will lead you to the very interesting project (you have the preview of the future
GWTnow): https://github.com/gwtproject/gwt-http. It is a future-proof port of the legacycom.google.gwt.http.HTTPGWT module. It will help to migrate GWT2.x projects to GWT3.x.When you look to the test package, you will find some examples: https://github.com/gwtproject/gwt-http/tree/master/src/test/java/org/gwtproject/http/client . So this is finally the answer to your question: "how to use it?" :-)
An additional examples source for
XMLHttpRequest(usingElemental2) from Gist: https://gist.github.com/search?utf8=%E2%9C%93&q=elemental2.dom.XMLHttpRequest. This is probably even better for start, as they are short and clear.What Elemental2 is?
The
Elemental2gives you a type checked access to native browser's API. So if you are familiar with browser's API, you should be able to implement your stuff, even based on some native JavaScript example. Please think about the new GWT like about type safe JavaScript (in addition very performant and well optimized). WithJsInteropyou create bindings, so it is something similar to bindings for TypeScript. So in fact you have a possibility to deal directly with browser's API, without anything GWT specific.Libraries? More examples...?
Dealing with
XMLHttpRequestis a bit low level.You have also a possibility to use the library. One of Github search results will lead you to this repository: https://github.com/ibaca/autorest-streaming-example which is an example for interesting REST library: https://github.com/intendia-oss/autorest. A modern and reactive one, works with Observables, RxJava and so on. This library uses
JsInteropand is also migrated toElemental2what makes itGWT3/J2CLready, please see the change: https://github.com/intendia-oss/autorest/commit/58516802cd42134544e6e3787207b5431fae94b5 .With the Github search query I provided you, now you are able to find even more code examples for
XMLHttpRequest. So please just have a look and find the best one for your needs.An alternative approach would be to use a framework, for instance Errai from RedHat: http://erraiframework.org/. It helps you to deal with many problems at a different abstraction level.
I think now you have some references to study.
On the other hand it's 2018, so why not the Fetch API?
When think about the modern web application, I would rather think about the
Fetch APIinstead ofXMLHttpRequest. All modern browsers now implement thefetch()function natively. Isn't it the best way for solving your issue then? Thefetch()is a Promise-based mechanism that allows you to make network requests similar toXMLHttpRequest. Promises and Fetch are handled by Elemental2. Then you can use it from your Java code more or less in similar way like in Mozilla's examples.Read more about the
Fetch APIhere:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
https://developers.google.com/web/updates/2015/03/introduction-to-fetch
https://codepen.io/aderaaij/post/fetching-data-with-fetch
https://fetch.spec.whatwg.org/
What more, this is nothing new as you see. If about the older browsers a
polyfillwill emulate the missing function: https://github.com/github/fetch.If about examples, I don't see that much on Github: https://github.com/search?utf8=%E2%9C%93&q=elemental2.dom.DomGlobal+fetch&type=Code, but at least something.
The
Fetch APIseems to be the most current solution to the problem.Please find a very simple fetch() example using Elemental2.
The imports section:
Then use in your code:
As a result you should be able to see something like this:
You can further convert the result to Java object using a technique called JsInterop DTOs. If you are interested, please find some information here: https://stackoverflow.com/a/50565283/5394086 .
Not recommended approach
If you sadly prefer to use the old GWT, so <= 2.7, then I think you can search for some examples on Github using similar search query, but for this legacy com.google.gwt.xhr.client.XMLHttpRequest. I this case I would also suggest you to not do stuff so low level, but use a library like https://github.com/reinert/requestor (which is unfortunately discontinued and development has stopped on GWT 2.7, but for this GWT version it is probably the best choice). But again please do not go this way and use GWT >= 2.8.2 with
Elemental2/JsInteropapproach instead.