我在iPhone上使用json-framework连接到在asp.net中构建的web服务。有时asp.net会在Web服务上抛出错误。但我找不到在iPhone上处理它们的方法,因为它们似乎不被NSURLConnection识别为错误。在查看响应数据时,它显示了json格式的错误,我怎样才能以一种简单的方式处理这个错误?在iPhone上处理json响应错误
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.
希望你明白我在说什么,只是想一个简单的方法来检查是否错误然后处理它,也许显示一个警告为用户或什么。谢谢!
为什么你的web服务引发错误,你不要处理?我认为你sohuld处理你的异常服务器端并返回告诉你错误发生的东西,不只是让错误传播给调用者。 – Daniel 2009-10-01 15:43:52
谢谢!是的,这正是我想要做的,但我不确定在发生错误时要返回给用户的是什么。如果我在服务器上发生错误,并且我可以在客户端上做什么,应该返回什么? – Martin 2009-10-01 16:27:58
你可以返回一个错误字符串或类似的东西 - 也许有一个代码(你将不得不列出什么代码意味着什么) – Daniel 2009-10-01 18:01:20