简体   繁体   English

如何在GWT中使用XMLHttpRequest?

[英]How to use XMLHttpRequest in GWT?

XMLHttpRequest is an alternative for HTTP calls from GWT client side and allows the control over all aspects of requests/responses. XMLHttpRequest是来自GWT客户端的HTTP调用的替代方案,允许控制请求/响应的所有方面。 But how to use it? 但是怎么用呢? javadoc address: http://www.gwtproject.org/javadoc/latest/com/google/gwt/xhr/client/class-use/XMLHttpRequest.html javadoc地址: http//www.gwtproject.org/javadoc/latest/com/google/gwt/xhr/client/class-use/XMLHttpRequest.html

You haven't mentioned what GWT version you use, so I assume the latest one. 你没有提到你使用的GWT版本,所以我假设最新版本。 It means 2.8.2 or newer. 这意味着2.8.2或更新。

Elemental2 is the way to go Elemental2是要走的路

As it is mentioned in comments above, Elemental2 is the right way. 正如上面的评论中提到的, Elemental2是正确的方法。 I will explain it a bit. 我会解释一下。

If you think about future-proof implementation (being aware of GWT3 / J2CL new approach), please do not use legacy GWT stuff. 如果您考虑面向未来的实现(了解GWT3 / J2CL新方法),请不要使用传统的GWT。 It means please use elemental2.dom.XMLHttpRequest instead of com.google.gwt.xhr.client.XMLHttpRequest (the one mentioned by you). 这意味着请使用elemental2.dom.XMLHttpRequest而不是com.google.gwt.xhr.client.XMLHttpRequest (您提到的那个)。 Please do not use gwt-user dependency if possible, as it will be deprecated (if it is not already). 如果可能,请不要使用gwt-user依赖项,因为它将被弃用(如果尚未弃用)。

The Elemental2 is an opensource project available here: https://github.com/google/elemental2 . Elemental2是一个开源项目: https//github.com/google/elemental2 It is kind of a base library for the "new GWT". 它是“新GWT”的基础库。 For easier migration of existing GWT2.x projects to GWT3.x, a part of the "old" gwt-user is currently being ported to the new approach using JsInterop technique and mentioned Elemental2 . 为了更容易地将现有GWT2.x项目迁移到GWT3.x,“旧” gwt-user的一部分目前正在使用JsInterop技术移植到新方法并提到了Elemental2 So definitely Elemental2 is the way to go. 所以, Elemental2绝对是您要走的路。

Elemental2 and JsInterop in general Elemental2和JsInterop一般

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 关于新的JsInterop方法,规范还不够丰富,但目前你至少会找到一些介绍: http//www.gwtproject.org/doc/latest/DevGuideCodingBasicsJsInterop.html

Examples 例子

Please find an example for XMLHttpRequest in this article: http://www.g-widgets.com/2016/09/09/gwt-http-requests-alternatives/ 请在本文中找到XMLHttpRequest的示例: 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 . 如果您查找示例,也可以通过以下方式在Github站点上搜索此示例: 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..." ) (要使用Github搜索,您需要登录,在其他情况下,您会看到“哇那里!你已经触发了滥用等等......”)

One of the results will lead you to the very interesting project (you have the preview of the future GWT now): https://github.com/gwtproject/gwt-http . 其中一个结果将引导您进入一个非常有趣的项目(您现在可以预览未来的GWT ): https//github.com/gwtproject/gwt-http It is a future-proof port of the legacy com.google.gwt.http.HTTP GWT module. 它是传统com.google.gwt.http.HTTP GWT模块的面向未来的端口。 It will help to migrate GWT2.x projects to GWT3.x. 它有助于将GWT2.x项目迁移到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 . 当你查看测试包时,你会发现一些例子: 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 (using Elemental2 ) from Gist: https://gist.github.com/search?utf8=%E2%9C%93&q=elemental2.dom.XMLHttpRequest . 来自Gist的XMLHttpRequest (使用Elemental2的另一个示例源: https ://gist.github.com/search utf8 =% E2%9C%93 q = Elemental2 This is probably even better for start, as they are short and clear. 这开始可能更好,因为它们很短且清晰。

What Elemental2 is? Elemental2是什么?

The Elemental2 gives you a type checked access to native browser's API. Elemental2为您提供了对本机浏览器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. 因此,如果您熟悉浏览器的API,即使基于一些本机JavaScript示例,您也应该能够实现您的内容。 Please think about the new GWT like about type safe JavaScript (in addition very performant and well optimized). 请考虑新的GWT类似于类型安全的JavaScript(此外非常高效和优化)。 With JsInterop you create bindings, so it is something similar to bindings for TypeScript. 使用JsInterop可以创建绑定,因此它类似于TypeScript的绑定。 So in fact you have a possibility to deal directly with browser's API, without anything GWT specific. 所以事实上你有可能直接处理浏览器的API,没有任何特定的GWT。

Libraries? 图书馆? More examples...? 更多例子......?

Dealing with XMLHttpRequest is a bit low level. 处理XMLHttpRequest程度有点低。

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 . Github搜索结果之一将引导您访问此存储库: https//github.com/ibaca/autorest-streaming-example这是有趣的REST库的示例: https//github.com/intendia-oss/autorest A modern and reactive one, works with Observables, RxJava and so on. 一个现代的和被动的,与Observables,RxJava等一起使用。 This library uses JsInterop and is also migrated to Elemental2 what makes it GWT3 / J2CL ready, please see the change: https://github.com/intendia-oss/autorest/commit/58516802cd42134544e6e3787207b5431fae94b5 . 这个库使用JsInterop并且也迁移到Elemental2 ,使它成为GWT3 / J2CL准备好,请参阅更改: 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 . 通过我为您提供的Github搜索查询,您现在可以找到更多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/ . 另一种方法是使用框架,例如RedHat的Errai: 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? 另一方面它是2018年,为什么不是Fetch API?

When think about the modern web application, I would rather think about the Fetch API instead of XMLHttpRequest . 在考虑现代Web应用程序时,我宁愿考虑Fetch API而不是XMLHttpRequest All modern browsers now implement the fetch() function natively. 所有现代浏览器现在都本机地实现了fetch()函数。 Isn't it the best way for solving your issue then? 那不是解决问题的最佳方法吗? The fetch() is a Promise-based mechanism that allows you to make network requests similar to XMLHttpRequest . fetch()是一种基于Promise的机制,允许您发出类似于XMLHttpRequest网络请求。 Promises and Fetch are handled by Elemental2. Promises和Fetch由Elemental2处理。 Then you can use it from your Java code more or less in similar way like in Mozilla's examples. 然后,您可以像在Mozilla的示例中一样,或多或少地使用Java代码中的它。

Read more about the Fetch API here: 在此处阅读有关Fetch API更多信息:

What more, this is nothing new as you see. 更重要的是,这并不像你看到的那样新鲜。 If about the older browsers a polyfill will emulate the missing function: https://github.com/github/fetch . 如果关于旧浏览器, polyfill将模拟缺少的函数: 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. 如果有关于示例,我在Github上看不到那么多: https//github.com/search? utf8 =% E2%9C%93 q = elemental2.dom.DomGlobal + fetch type = Code,但至少有些东西。

The Fetch API seems to be the most current solution to the problem. Fetch API似乎是该问题的最新解决方案。

Please find a very simple fetch() example using Elemental2. 请使用Elemental2找到一个非常简单的fetch()示例。

The imports section: 进口部分:

import static elemental2.dom.DomGlobal.fetch;
import static elemental2.dom.DomGlobal.console;
import elemental2.dom.Response;

Then use in your code: 然后在你的代码中使用:

    fetch("https://randomuser.me/api/?gender=female&results=1")
            .then(Response::json)
            .then(data -> {
                console.log(Global.JSON.stringify(data));
                return null;
            }).
            catch_(error -> {
                console.log(error);
                return null;
            });

As a result you should be able to see something like this: 结果你应该能够看到这样的东西:

{"results":[{"gender":"female","name":{"title":"mrs","first":"caroline","last":"coleman"},"location":{"street":"3703 new road","city":"swansea","state":"leicestershire","postcode":"ZH67 0YS","coordinates":{"latitude":"14.7870","longitude":"-107.8990"},"timezone":{"offset":"-6:00","description":"Central Time (US & Canada), Mexico City"}},"email":"caroline.coleman@example.com","login":{"uuid":"25357d90-cce4-4fe6-a3db-8ab77c0272ba","username":"smallpeacock582","password":"citizen","salt":"VX3s05Ah","md5":"84649cce1db8c6f2cbe33098221aa570","sha1":"005abf7d2ca0ff5b1a0bfd6dcee6d4860ef6e75d","sha256":"caadff0a16e27b0d9893aea483aedc7cf7c4707096c33a58acf44336bb2b54be"},"dob":{"date":"1978-03-14T15:47:16Z","age":40},"registered":{"date":"2013-08-10T19:09:41Z","age":5},"phone":"015396 74385","cell":"0726-723-103","id":{"name":"NINO","value":"JA 32 24 22 P"},"picture":{"large":"https://randomuser.me/api/portraits/women/45.jpg","medium":"https://randomuser.me/api/portraits/med/women/45.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/45.jpg"},"nat":"GB"}],"info":{"seed":"98f4f4a344470fbd","results":1,"page":1,"version":"1.2"}}

You can further convert the result to Java object using a technique called JsInterop DTOs. 您可以使用名为JsInterop DTO的技术将结果进一步转换为Java对象。 If you are interested, please find some information here: https://stackoverflow.com/a/50565283/5394086 . 如果您有兴趣,请在此处找到一些信息: 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. 如果你不喜欢使用旧的GWT,那么<= 2.7,那么我认为你可以使用类似的搜索查询在Github上搜索一些例子,但是对于这个遗留的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). 在这种情况下,我还建议你不要做那么低级别的东西,但是使用像https://github.com/reinert/requestor这样的库(不幸的是停止了,并且开发已经停止在GWT 2.7上,但对于这个GWT版本它可能是最好的选择)。 But again please do not go this way and use GWT >= 2.8.2 with Elemental2 / JsInterop approach instead. 但请不要这样做,而是使用GWT> = 2.8.2与Elemental2 / JsInterop方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM