ConnectionRequest and reading the Result

119 views
Skip to first unread message

steve

unread,
Apr 2, 2014, 6:20:15 AM4/2/14
to codenameone...@googlegroups.com
Hi,
I'm trying to make an app that connects to a server through an API and the server responds...The problem is that i do not get the full xml response ie. when i use Post Master i get the following output:
<sentMessages>
    <message>
        <messageID>1396427838558</messageID>
        <text></text>
        <successful></successful>
        <failed></failed>
    </message>
    <message>
        ...
    </message>
    <message>
       ...
    </message>
    <message>
        ...
    </message>
</sentMessages>
<count>20</count>
<lastOffset>20</lastOffset>


but in my app i don't get the :
<count>20</count>
<lastOffset>20</lastOffset>

QUESTIONS.
1.What could be the issue?
2.How can i get and use the Result?...When it's simple output I can be able to use the output (I use Result.getAsString()),bt an array type output like above EDT error occurs not quite sure how to use (Result.getAsStringArray())...any help will be greatly appreciated...

Here is my code (it prints out the Result but i cannot use it and still do not get part of the server Result):

System.out.println("fetching more...");
        NetworkManager networkManager = NetworkManager.getInstance();
        networkManager.start();
        networkManager.addErrorListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                NetworkEvent n = (NetworkEvent) evt;
                n.getError().printStackTrace();
            }
        });

        String encodeOffset = Util.encodeBody("0");

        String encodeRowCount = Util.encodeBody(Storage.getInstance().readObject("rowCount").toString());
        String url = "url...?rowCount=" + encodeRowCount + "&lastOffset=" + encodeOffset;

        final ConnectionRequest request = new ConnectionRequest() {

            @Override
            protected void readResponse(InputStream input) throws IOException {

                Result evaluator = Result.fromContent(input, "xml");
                
                System.out.println(evaluator);
        //tried a for loop to copy the data using getAsStringArray() got EDT errors...

            }
        };
        request.setPost(false);
        request.addRequestHeader("userID", Storage.getInstance().readObject("userid").toString());
        System.out.println(Storage.getInstance().readObject("userid").toString());
        request.setUrl(url);

        request.addResponseCodeListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                Dialog.show("Error", "Oops...something seems to have gone wrong. We apologise for "
                        + "the inconvenience.", "OK", null);
            }
        });

        networkManager.addToQueueAndWait(request);

PS.reading this fine article for the expression language xml consumption ...

Thanks in Advance,
Steve

steve

unread,
Apr 2, 2014, 7:02:12 AM4/2/14
to codenameone...@googlegroups.com
Hi,
 Just to add when i usse the network monitor i can view all the content but the evaluator does not print it out...

Shai Almog

unread,
Apr 2, 2014, 1:37:32 PM4/2/14
to codenameone...@googlegroups.com
Hi,
that's not valid XML.
XML has one root element.

You can just read the full string by reading the input stream but you will have a problem parsing it.
You can patch this on the device by wrapping this invalid XML in an additional entity assuming the rest is well formed.

steve

unread,
Apr 2, 2014, 11:45:13 PM4/2/14
to codenameone...@googlegroups.com
Hi,
Thanks for the quick response, i have a follow up question...How does one wrap the invalid XML in an additional entity?...


Shai Almog

unread,
Apr 3, 2014, 1:19:48 AM4/3/14
to codenameone...@googlegroups.com
Hi,
e.g.
String xml = "<dummy>" + Util.readToString(input) + "</dummy>";


Then parse that string.

steve

unread,
Apr 3, 2014, 4:25:56 AM4/3/14
to codenameone...@googlegroups.com
Hi,
Thanks for the quick reply...it works ...im trying to loop through the xml output (it's now a Result object)... but i get this error: java.lang.IllegalStateException: Could not create a comparator for value:

This is the code:
for (int i = 0; i < 5; i++) { System.out.println(evaluator.getAsString("/dummy/sentMessages/message[i]/text").toString()); }




When i try this out it prints that specific value 5 times: 
for (int i = 0; i < 5; i++) { System.out.println(evaluator.getAsString("/dummy/sentMessages/message[4]/text").toString()); }

Is there a way of looping through the xml output getting the different values (or do i have to code the numbers manually?), or where am i going wrong?...

Thanks in advance,
Steve.

Shai Almog

unread,
Apr 3, 2014, 2:07:33 PM4/3/14
to codenameone...@googlegroups.com
Hi,
I'm not very good with the processing package but it has a get as array method which is probably closer to what you are looking.
Reply all
Reply to author
Forward
0 new messages