In Mobile development, how to get the result of a REST service?

Hello,
I have built a simple rest resource in the software AG Designer at Service Development,
and I add to this, a simple flow service, which has 2 string inputs , a concat function only, and outputs a string list.
I can obtain the result of this service by running it in the web browser I see in the result of this rest query, which
is a table with the result values of the pipeline of this service.
So it doesn’t seem to be any problem with the REST Resource.

Now in the side of the Mobile Development of Software AG Designer, I am trying to write on the text of a textfield to call
the REST method by writing @{methodName} but I always get an empty space as a result. (I always generate the code and then run the emulator)

Anyone has an idea of what the problem can be about?

Hi,

it’s always hard to figure out whats going wrong without any knowledge of the project. So, if its possible, could you attach your project here?

In general, It’s not supported to run a RESTful service automatically when you enter something in an Entry. You’ll have to write a bit of Java Code to make this working. I’m also not sure whats your experience with Mobile Suite so far. Please let me know if my explanation is too short.

For your use case, I would create an Entry and a Button in a View. When creating a new Mobile Project, you already have a “ready-to-use” project containing one View, the MasterView. Please open this View in the Outline Editor and add an Entry and Button. You can right click on an Element inside the Outline Editor and select “New Child”. You could also drag and drop the desired Element from the palette view and drag it to the target View. Please make sure to assign the “Name” property to both Elements. This step is a requirement to work with both Elements in your Java code.

Afterwards, you need to configure the Button to react on an Event. In this example, please add a TriggerListener to the Button. This type of Listener will fire an Event when a user taps the Button. After adding the TriggerListener, please a Delegate to the Listener. The Delegate is a predefined event type, which calls the specified Java method (Property “Method Name” in Delegate). As an example, use “onButtonClicked” as value for the property “Method Name” in Delegate. So, when a user taps on the Button, the method onButtonClicked will be invoked and you can fill in your business logic.

Now, generate your source code and dive into the Java code. Either locate the View controller class (MasterViewControllerImpl.java for new Projects) or select the View in the Outline Editor, do a right click and select “Open Java Source”.

In your Java class, you have to overwrite the Delegate method. In our example, it’s “onButtonClicked”. The signature should be:

public void onButtonClicked(final nUIButtonElement sender);

Finally, you can call your RESTful service within the method body. I assume you already created the Resource and the Method in the Outline Editor? If thats the case, Mobile Development creates a Java class, named like your Method. To proceed with our example, lets assume the Method is named “ConcateString”. So, the correspondent Java class to fire up a request is name “ConcateString.java” and is located in:

gen/src/<yourBundleId>/services/rest/ConcateString.java.

If you have not created a Resource and a Method yet, please drag the “Resources” object (from the “RESTful Services” category in your palette View) on top the “Services” container in the tree editor. Afterwards, a wizard will be opened where you can enter all required properties for this service. E.g. path to the resource on IS, the HTTP Method, and the Java-Method-Name.

So, back to your Java code, create a new instance of your Method:

final ConcateString service = new ConcateString();

It’s worth to know that this class inherits from com.softwareag.mobile.runtime.toolkit.rest.AbstractRestOperation. You’ll find AbstractRestOperation in the gen/API-src folder. It provides a bit of Java Doc to help you creating the service. However, to start the request, just call the execute() method. This will finally invoke your service.

You could also fill in the text, a user wrote inside the Entry. Therefore, access the entered text via:

final String inputValue = getView().get<TheNameOfYourEntry>().getText();

Passing this value to your service is dependent on your actual service design. For POST requests, you can invoke the method setPostData() on your service to pass any data you’d like. If your service exposes Parameters, Mobile Development create a setter for every Parameter in your service class and in addition, another execute method, where you can pass all the Parameter values.

Last, but not least. You might like to know whether your service executes successfully. For that reason, please have a look at the API class com.softwareag.mobile.runtime.toolkit.delegates.IOperationDelegate. It acts as a listener for services. In your code, do the following:


final ConcateString service = new ConcateString();
service.addOperationDelegate(new IOperationDelegate() {
			
			public void onOperationFinished(IOperation operation) {
			}
			
			public void onOperationFailed(IOperation operation) {
			}
		});

Let me know if that helps or if you need further assistance.

Best Regards
Jörg

Mobile Designer Development Team

I attached my project with this reply. I had some doubts concerning the code part and your answer, around where is to write the
"gen/src//services/rest/ConcateString.java.

I attached my project with this reply. I had some doubts concerning the code part and your answer, around where is to write the
“gen/src//services/rest/ConcateString.java”

Hi Ana,

I don’t see an attachment. Could you e-mail me? joerg.hartmann@softwareag.com

I supposed thats the name of the REST method in the model. In this case, Mobile Development creates Java class ConcateString.java, which you can initiate and invoke to get your IS service running. You don’t have to code that class.

Best Regards
Jörg

I attached my project with this reply. I had some doubts concerning the code part and your answer, around where is to write the
“gen/src//services/rest/ConcateString.java”

I got some code commented because it was giving me it errors.
it runs with the code commented though.
NewProjectTest.rar (1.17 MB)

I believe I am closer to the solution now
so I send my project again as well as an image of the error I am getting.

everytime I click this button the console writes this error:
"

PM com.softwareag.mobile.runtime.comms. HttpConnectionHandler run
SEVERE: Could not connect .

"

Please check the attachments,


NewProjectTestr1807.rar (1.18 MB)

Hi Ana,

how are you doing? Did you get your service working?

Regards
Jörg