How do I figure out when the contents of NSData indicate that a webservice request has failed?

63 views Asked by At

I'm trying to persist data from a microsoft web service call to a file when my app starts up. If the app is able to complete the webservice request, it's storing the data in an NSData object. Assuming the data has been successfully requested and stored, I want to execute certain code that I would NOT want to if the webservice is unsuccessful.

My webservice request is as follows:

    NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc]init];
    [request1 setURL:[NSURL URLWithString:@"---URL---"]];
    [request1 setHTTPMethod:@"GET"];

    NSURLResponse *request1Response;
    NSData *request1Handler = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&request1Response error:nil];
    NSString *request1Reply = [[NSString alloc]initWithBytes:[request1Handler bytes] length:[request1Handler length] encoding:NSASCIIStringEncoding];
    NSData *data1 = [request1Reply dataUsingEncoding:NSUTF16StringEncoding];

So basically, the response is dropped into that data1 object.

When connected to the internet, the request executes fine. The code that follows is wrapped in a if(data1){ conditional to make sure that the webservice request is successful before executing it. The problem is that when I disconnect from the internet(and cut off access to that webservice), the code inside the conditional is still being executed.

I tried comparing data1 to nil, logging data1 to do a direct comparison of the contents, etc, but I gather that that data1 object isn't nil; it probably contains some sort of failure message that I have thus far been unable to access. What can I do in the conditional or in the webservice request itself to figure out when the request fails?

2

There are 2 answers

1
user80755 On BEST ANSWER

Create an NSError object and pass it as argument to the sendSynchronousRequest: method, then if there is network or another error, the err object will populated with error information hence it will not be nil. That means you can check if(!err)contiune else there is an error

check the code:

  NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc]init];
    [request1 setURL:[NSURL URLWithString:@"http://www.google.com"]];
    [request1 setHTTPMethod:@"GET"];
    //object where error will be saved 
    NSError *err;
    NSURLResponse *request1Response;
    NSData *request1Handler = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&request1Response error:&err];//pass the err here by reference
    NSString *request1Reply = [[NSString alloc]initWithBytes:[request1Handler bytes] length:[request1Handler length] encoding:NSASCIIStringEncoding];
    NSData *data1 = [request1Reply dataUsingEncoding:NSUTF16StringEncoding];

and then you can check here:

if (err){
NSLog(@"error: %@", err);
}
0
Wain On

You should at least be checking for an error response by populating the error pointer parameter.

Ideally you should be using a different API which gives you access to more details such as the response HTTP status code which you should be using to determine what happened even if you did get something which looks like success.