REST web service failure

63 views
Skip to first unread message

anop...@gmail.com

unread,
Nov 13, 2013, 9:06:38 AM11/13/13
to codenameone...@googlegroups.com
I'm trying to make a REST web service call (used for login).  When the URL is correct everything works fine on the emulator.

The part of my code used to call the service is the following:

String url = "http://....";

ConnectionRequest con = new ConnectionRequest();

con.setUrl(url);
con.setPost(false);
con.setContentType("application/json");

InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
con.setDisposeOnCompletion(dlg);

NetworkManager.getInstance().addToQueueAndWait(con);
byte[] data = con.getResponseData();
String response = openFileToString(data);

InputStream is = new ByteArrayInputStream(response.getBytes());

When the username (which is part of the URL) does not exist the following takes place:

After making the call, the application displays a "500: Internal Server Error" dialog on the emulator.
Specifically when running in DEBUG mode the code stops at the following line:
NetworkManager.getInstance().addToQueueAndWait(con);

However, trying the same URL from my browser gives back:
{"isError":true,"message":"ENOENT,open 'C:\\testing\\myFile.json'"}

How can I manipulate the error properly in my CN1 code?

Thanks (I've check a relevant post concerning the same matter but didn't help me much)

Shai Almog

unread,
Nov 13, 2013, 1:55:57 PM11/13/13
to codenameone...@googlegroups.com, anop...@gmail.com
You have two separate questions here. The first is the error message you get from Codename One which you can resolve via:
1. Invoke con.setFailSilently(true)
2. Override protected void handleErrorResponseCode(int code, String message)
3. Add an error handler to NetworkManager and consume the event


The second issue is that the stream is effectively cut off and you don't get the actual data from the stream (this is by convention in Java) you can workaround this by invoking con.setReadResponseForErrors(true);
Reply all
Reply to author
Forward
0 new messages