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
There are examples of the use of the Httpconn class in this sample repository
– Jefferson Quesado
I tried to use the example that was passed. https://github.com/TotalCross/tcrest. But you are error in dependencies.
– Alessandro
@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
– Jefferson Quesado
Jefferson, it wasn’t very clear about this Jsonsimple issue. Could you give me an example of how to do this?
– Alessandro
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 aContentHandler
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 Quesado
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?
– Alessandro
Unfortunately, we don’t support java.awt.Color =/ You have some control over the library?
– Jefferson Quesado
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.
– Alessandro