1
votes

I have written an app with CNO that connects to a webservice using this piece of code...

NetworkManager.getInstance().addToQueueAndWait(connReq);
Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(connReq.getResponseData()), "UTF-8"));
return result; 

The code works fine for all successful connections. However when there is a "500 Internal Server Error" all it does it display a dialog with "Cancel" and "Retry".

The problem is that I am deliberately firing a bad request at an endpoint as part of a test - but I don't seems to have any choice about how to handle a bad response.

The code gets to this line - NetworkManager.getInstance().addToQueueAndWait(connReq); - and then dies.

Is there anyway to capture a fail and then carry on?

Thanks

1

1 Answers

0
votes

There are a lot of ways to handle errors. Check out the developer guide section on networking for a lengthy discussion.

A simple solution consistent with your approach is:

connReq.setFailSilently(true);
NetworkManager.getInstance().addToQueueAndWait(connReq);    

if(connReq.getResponseCode() == 200 || connReq.getResponseCode() == 202) {
    // success code here
} else {
    // fail code here...
}