views:

92

answers:

1

I'm using json-framework on iPhone to connect to a web service built in asp.net. Sometimes asp.net throws errors on the web service. But I can't find a way to handle them on the iPhone as they doesn't seem to be recognised as errors by NSURLConnection. When looking at the response data it shows the error in json format, how can I in an easy way handle this error?

NSError* requestError = nil;
NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&requestError];

//requestError is still nil even if I get asp.net errors on the server.

SBJSON *parser = [[SBJSON alloc] init];
NSString *jsonResponseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *jsonParserError = nil;
NSDictionary *jsonResponse = [parser objectWithString:jsonResponseString error:&jsonParserError];

//jsonParserError is still nil because the response data is in json format, so it wont recognise the error.

Hope you understand what I'm talkning about, just want an easy way to check if error then handle it, maybe show an alert for the user or something. Thanks!

A: 

The error object you pass by reference to the JSON parser is for catching errors that occur during the actual parsing of the string - not for handling errors sent from your web service.

Your JSON message should contain the error information sent from the server. If this is the case, all you need to do is parse the message, and then check the resulting NSDictionary's for the particular error key that should be defined in the message.

If that key exists in the dictionary, and its value is true (say for error:true vs error:false) then you have an error and you should handle it.

It really is dependant on what the web service sends back to the client in the case of an error.

The basic idea is that it should send a JSON message with some information, possibly in a similar format to:

{error:1,errorMessage:"Something went wrong"}
Jasarien
Thanks! Thats what I was thinking I could to, have changed it and it works perfect!
Martin