How can I send Java object between client and server on Android and iOS?

爱⌒轻易说出口 提交于 2019-12-05 18:31:24

In terms of client-server communication you can have a look at Gluon Connect and Gluon CloudLink.

Gluon Connect

An open source library:

Gluon Connect is a client-side library that simplifies binding your data from any source and format to your JavaFX UI controls. It works by retrieving data from a data source and converting that data from a specific format into JavaFX observable lists and observable objects that can be used directly in JavaFX UI controls.

It is also part of the Charm dependencies, so you have it already included when you create a new Gluon project.

See the documentation on how to use to create a FileProvider or a RestProvider, and also the GluonConnectRestProvider sample.

As the doc already mentions: with a RestClient you can "convert" a REST endpoint into an ObservableList:

// create a RestClient to the specific URL
RestClient restClient = RestClient.create()
    .requestMethod(RestClient.Method.GET)
    .host("https://api.stackexchange.com")
    .path("/2.2/errors");

// retrieve a list from the DataProvider
GluonObservableList<Error> errors = DataProvider
    .retrieveList(restClient.buildListDataReader(Error.class));

// create a JavaFX ListView and populate it with the retrieved list
ListView<Error> lvErrors = new ListView<>(errors);

The Notes sample uses Gluon Connect for local storage of Notes and Settings.

Note that these samples make uses of JavaFX POJOs (i.e. Error, Note and Settings use properties).

Gluon CloudLink

Gluon CloudLink enables enterprise and mobile developers to easily connect their different services and applications together, enabling bi-directional communications between mobile apps, enterprise infrastructure, and cloud systems.

The data is stored in the cloud, and you (as administrator) can access to it through a Dashboard.

See documentation about it here.

Have a look at the PWS-GluonCloudLink-Whiteboard sample: a full demo of a back-end application (webapp-mobile) running on the cloud (Pivotal Web Services) and a mobile client (mobile app).

In the client side, once you get a valid GluonClient, you can retrieve an observable list of items:

public GluonObservableList<Item> retrieveItems() {
    GluonObservableList<Item> items = DataProvider.retrieveList(gluonClient.createListDataReader("items", Item.class));
    return items;
}

As you can see, in the client you don't deal with REST endpoints, json serializing... Everything is just JavaFX observables. The connection with the backend is set in CloudLink with the Dashboard application, defining a REST Connector.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!