I'm trying to get this UIAlertView to run when fetching the JSON data fails, but i can't seem to get it to work. I'd appreciate it if someone could show me how to do it or point me in the right direction!
Asked
Active
Viewed 74 times
-3
-
Fails how? Provide more details about what is actually happening versus what you expect to happen. – rmaddy Jun 29 '15 at 18:10
-
Also note that you must perform UI work on the main thread. – rmaddy Jun 29 '15 at 18:10
-
check response object header. In that check the status code.200 or 400 or whatever and act accordingly – Rajan Maheshwari Jun 29 '15 at 18:13
-
If it fails to get the contents of the JSON (Not fundamental failures which are already accounted for by the completion/failure blocks). The self.jobs != nil is not doing anything. When i comment out the line "self.jobs = (NSArray *)responseObject;" , it works, but when i leave it in there and it fails, nothing happens. – Jgreen727 Jun 29 '15 at 18:14
-
#Jgreen727. Check your responseObject data type. Is it NSArray or anything else? like NSDictionary? May typeCasting getting failed. – Kirti Nikam Jun 29 '15 at 18:18
-
I updated it with more code if that helps you guys – Jgreen727 Jun 29 '15 at 18:25
-
#Jgreen727, Can you add you json response too in your question. – Kirti Nikam Jun 29 '15 at 18:28
-
Sometimes it fails and just returns a blank tableview, which i assume is it failing to retrieve the JSON data. That's what I'm trying to check for with the if self.jobs = nil block. The only issue is that is not working. – Jgreen727 Jun 29 '15 at 18:29
-
What do you mean my json response? – Jgreen727 Jun 29 '15 at 18:29
-
What you are getting in responseObject? – Kirti Nikam Jun 29 '15 at 18:31
-
are you getting sometimes this.. [ ] only? – Rajan Maheshwari Jun 29 '15 at 18:31
-
how would i check what I'm getting? – Jgreen727 Jun 29 '15 at 18:32
-
Do NSLog for it and print it in console. NSLog(@"%@", responseObject); – Kirti Nikam Jun 29 '15 at 18:33
-
I ran this service with some garbage values and got this in response [ ] Is this the case with you too? – Rajan Maheshwari Jun 29 '15 at 18:35
-
on failure i get [9853:2285091] ( ) – Jgreen727 Jun 29 '15 at 18:44
-
Try to check NSError of failure block. http://stackoverflow.com/a/19102233/1635315 – Kirti Nikam Jun 29 '15 at 18:56
2 Answers
0
I think you have forget to write failure block.
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
}];
Hope this will help you.

Kirti Nikam
- 2,166
- 2
- 22
- 43
-
I have a failure block already, i just didn't add it in here because it was irrelevant! The failure/success blocks only account for fundamental errors. – Jgreen727 Jun 29 '15 at 18:20
-
Ok, just now checked your comment. Check my comment below your question. – Kirti Nikam Jun 29 '15 at 18:22
0
You can write this in your block
NSDictionary *headersCollection=[[(NSDictionary *)operation valueForKey:@"response"]valueForKey:@"allHeaderFields"];
NSMutableDictionary *headers=[headersCollection mutableCopy];
headers[@"statusCode"]=[NSNumber numberWithInteger:operation.response.statusCode];
and then check the status code value. Headers dictionary will provide you the complete header which is returned. If statuscode is 200 everything is OK else you can show your custom message accordingly with different status code values like 400, 415 etc

Rajan Maheshwari
- 14,465
- 6
- 64
- 98
-
-
1well u can do anything when u get the header and statuscode. As in the question it is not mentioned how json fails. It can fail in any context. Secondly if you want to pass this header dictionary to some other controller where you are executing the web service call, you can make the success block there and pass the dictionary of headers – Rajan Maheshwari Jun 29 '15 at 18:22