consume REST service with totalcross

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);

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

Exception: class java.lang.NegativeArraySizeException.

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

Another thing, if I have the following situation:

  • Group Class
  • Subgroup class

Where the Subgroup class has an object of type group. When using JSONFactory this way:

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

It gives error:

java.lang.IllegalAccessException: Can not call newInstance() on the Class for java.lang.Class
    
asked by anonymous 22.03.2017 / 13:53

2 answers

3

Alessandro,

In TotalCross's github you have an example of how to use the API for REST calls. link

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

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

    
22.03.2017 / 14:40
6
  

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

     

[...]

     

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

In the service definition, the server does not necessarily need to send the content size. To deal with these situations, in TotalCross we use contentLength == -1

We made a wrapper to handle this as best we could, even treating the party to receive a compressed response ( gzip or deflate ). The wrapper is found in the TotalCross utilities (class HttpConn ).

  

Another thing, if I have the following situation:

     

Class Group Class Subgroup

     

Where the Subgroup class has an object of type group, when using JSONFactory, like this:

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

For 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 this purpose. JSONFactory.parse can interpret correctly only if the parseled object does not have subobjects. So if Subgrupo has an object of type OutraClasse as tribute, a parser crash will occur.

EDIT

In TotalCross, we put the JSON Simple repository into the SDK, so lucky that we have a lightweight compiler (SAX-like) from JSON. To parse a JSON with this framework, you need to create a ContentHandler . In the original JSON Simple repository itself, you have examples of that . We also have an example of this inside a sample repository .

    
22.03.2017 / 14:29