-2

I use AFNetworking request server, but I get output like this:

2012-12-25 15:16:47.578 SmsForNewYear[3825:c07] <3c21444f 43545950 45206874 6d6c2050 55424c49 4320222d 2f2f5733 432f2f44 54442058 48544d4c 20312e30 20547261 6e736974 696f6e61 6c2f2f45 4e222022 68747470 3a2f2f77 77772e77 332e6f72

It's not json, if I add [self setDefaultHeader:@"Accept" value:@"application/json"];

It returns:

SmsForNewYear[3920:c07] getNewSms:Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html

But I used browser, it looks this

{"rs_code":"200","data":{"sms_cate_list":[{"id":"26","name":"\u559c\u5e86","likes":"0","sms_num":"1","order_id":"1","is_index":"0","update_time":"1355932115"}

It is json.

on the other hand, i use asihttprequest can return json.

why?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
19jk89
  • 13
  • 1
  • Incidentally, decoding that `NSData`, it looks like it's a start of an HTML response (but there's not enough here to see what it says). When you `NSLog` the `[[NSString alloc] initWithData:jsonData]`, it will be interesting to see what it is, but it could be an error message about the URL being wrong or something like that. – Rob Dec 25 '12 at 08:18
  • thanks,i found the wrong, it's no rest style ,i write like this [AFSmsAPIClient sharedClient] getPath:@"sn=sms.sms.display" parameters:nil , it is look like facebook style , so sn=sms.sms.display is get method's parameter – 19jk89 Dec 25 '12 at 08:30
  • Cool. It was impossible to diagnose on the basis of the `NSLog` of the `NSData`. I initially assumed (incorrectly) that it might have been a valid JSON response, but upon further inspection, it was clear that there was some error being reported by the server (but the `NSData` that you included was not enough to diagnose it because it was getting cut off). Anyway, good you found it. In the future, if you get a server response in a `NSData` object, converting it to a `NSString` like I outline below will help you diagnose the problem much more quickly. – Rob Dec 25 '12 at 08:32

1 Answers1

3

Your first NSLog is a typical NSData log result, which is hard to read. If you want to look at it for debugging purposes, you can

NSLog(@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

Obviously replace jsonData with whatever variable is holding that NSData object. You can examine that before you declare failure.

When I decode the NSData string that you logged, it appears to be the start of a HTML file, so I bet your web server is reporting an error. You'll have to look at the NSString version of that HTML to figure out what the problem is (you haven't included enough for us to diagnose, as the NSLog of the NSData is cutting off the result prematurely).

In terms of diagnosing the problem, you have to show us the message generated by the above NSLog. You probably also need to show us your AFNetworking code where you make the request of the web service.


Generally when you download data from a web server, when you NSLog the NSData result, you'll see something cryptic like this. You can then, quite happily, pass to NSJSONSerialization:

NSError *error = nil;
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:jsonData
                                                           options:0
                                                             error:&error];
if (error != nil)
    NSLog(@"Error in JSON: %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • [[AFSmsAPIClient sharedClient] getPath:@"sn=sms.sms.display" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) { NSLog(@"%@",operation.responseString);} i through this to found this wrong. – 19jk89 Dec 25 '12 at 08:32