consume REST service with totalcross

Asked

Viewed 396 times

5

I would like to know how to consume a REST service using totalcross.

I made an example following what is in the documentation, I am using Httpstream.

String url = enderecoWS + servico;
HttpStream hs = new HttpStream(new URI(url));
byte[] buf = new byte[hs.contentLength];
hs.readBytes(buf, 0, hs.contentLength);
String str = new String(buf);

Put in line byte[] buf = new byte[hs.contentLength], the value of contentLength is -1 and generates

Exception: class java.lang.NegativeArraySizeException.

I made a change to the code above by changing hs.contentLength for hs.readLine().length(), this way I can catch the return, but from a part of the string.

Another thing, in case I have the following situation:

  • Group Class
  • Subgroup class

Where the Subgroup class has a group object. When using the JSONFactory thus:

Subgrupo[] subgrupoArray = JSONFactory.parse(str, Subgrupo[].class);

Makes a mistake:

java.lang.IllegalAccessException: Can not call newInstance() on the Class for java.lang.Class

2 answers

6

I would like to know how to consume a REST service using totalcross.

[...]

Put in line "byte[] buf = new byte[hs.contentLength]", the value of contentLength is -1 and generates

In the service definition, the server does not necessarily need to send the content size. To handle these situations, Totalcross uses the contentLength == -1

We wrapped to treat this in the best possible way, including treating the part of receiving a compressed response (gzip or deflate). Wrapper is found in Totalcross utilities (class Httpconn).

Another thing, in case I have the following situation:

Class Group Class Subgroup

Where the Subgroup class has a group object, when using Jsonfactory, thus:

Subgrupo[] subgrupoArray = JSONFactory.parse(str, Subgrupo[].class);

To the JSONFactory, you are passing the array class. You should pass Subgrupo[] subgrupoArray = JSONFactory.parse(str, Subgrupo.class);

If Subgrupo is not a shallow object, I recommend using the Jsonsimple API for that purpose. The JSONFactory.parse can only interpret correctly if the parsed object has no subobjects. So, if Subgrupo have as tribute an object of type OutraClasse, there will be a fault in the parser.


EDIT

At Totalcross, we port the repository of JSON Simple into the SDK, so much so that we have a lightweight JSON compiler (SAX-like). To parse a JSON with this framework, you need to create a ContentHandler. In the original JSON Simple repository itself examples of this. We also have an example of this within an example repository.

  • There are examples of the use of the Httpconn class in this sample repository

  • I tried to use the example that was passed. https://github.com/TotalCross/tcrest. But you are error in dependencies.

  • @Alessandro, we’re working to upload all these codes to the Maven Repository Center. We uploaded the update with <code>Httpstream.Options</code> in pre.totalcross.com

  • 1

    Jefferson, it wasn’t very clear about this Jsonsimple issue. Could you give me an example of how to do this?

  • 1

    Ok, I had no reputation for multiple links (I’ll edit above also to make it clearer). Jsonsimple is a JSON compilation library in a quick and direct way to the subject. We use the compilation in this example. To make the copying, you must call the parser and pass on to him a ContentHandler created by you. Direct link to the parser we use: https://github.com/TotalCross/totalcross-big-file/blob/master/src/main/java/com/tc/sample/bigfile/ui/JsonSimpleShower.java#L56

  • Jefferson, another question for you. When deploying a test application that used a third-party library, I was shown an error message that a java import (java.lang.awt.Color) used in one class of this library should be replaced by another (I think of totalcross). I can get around that problem?

  • 1

    Unfortunately, we don’t support java.awt.Color =/ You have some control over the library?

  • Unfortunately I have no control, because it is the library of spring. I’m trying to do a test with this Jsonsimple example, but it’s a bit complicated to understand and adjust this in my project.

Show 3 more comments

3


Alessandro,

In Totalcross’s github there is an example of how to use the API for REST calls. https://github.com/TotalCross/tcrest

Try running the example and see if it meets what you need =)

Another important thing is that you download the latest version of the SDK on www.totalcross.com always has API updates/fixes

  • Bruno, these "com.tc.ui" and "com.tc.utils" dependencies are not in the Maven central repository. Where can I download them?

  • 1

    Alessandro, put those projects on the github. They are the missing dependencies https://github.com/TotalCross/tc-utilities https://github.com/TotalCross/tc-components-renewed https://github.com/TotalCross/magical-utils

  • I imported all the projects, but it is error in the Tc-Utilities project class "Httpconn" in line : opt.setSendData(true);

  • Bruno, I noticed that some examples that were passed, have reference to classes of the Java api (JDK) when I will deploy occurs error due to these Imports. What I need to do to fix this?

  • 1

    Hi Alessandro, I made two small modifications in the projects. You can pull the changes and test?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.