-2

I need to send my login and pass to web server in data variable in JSON via POST I tried :

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"email" : email, @"password" : pass };
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error); }];

but have:

Data = "<null>";
Message = "unexpected end of JSON input";
Result = fail;
user3196922
  • 115
  • 2
  • 9
  • 1
    @Rob omg, so it was the same guy? When researching for this question I came across two other questions that looked exactly the same as this but had different titles so didnt think much of it ! wow, disgusting. – Pavan Jan 16 '14 at 04:27

1 Answers1

0

You need to convert your dictionary into a JSON object. Atm sending a dictionary via the params will not work hence your error.

Consider using the SBJson library which can convert your dictionary into a JSON string object and can also parse JSON strings into dictionaries again.

SBJson framework

Then you simply include the header files like so:

#import "SBJson.h

And then you will be able to use the category method on your dictionaries like so:

NSDictionary *params = @{@"email" : email, @"password" : pass };
NSString *jsonString = [params JSONRepresentation];
//and then you send the jsonString as the params
Pavan
  • 17,840
  • 8
  • 59
  • 100
  • 2
    AFNetworking (using that `requestSerializer`) does the JSON conversion for him. AFNetworking takes a `NSDictionary`, not JSON. (And even if he had to do the JSON conversion himself, nowadays people would generally recommend the built-in `NSJSONSerialization` rather than third-party JSON libraries, unless you had to support iOS 4.x.) – Rob Jan 16 '14 at 04:29
  • @Rob Oh wow! the result of AFNetworking 2.0? Are there any other major benefits for me to reconsider updating the AFNetworking library across all of my client's apps?! – Pavan Jan 16 '14 at 04:40
  • AFNetworking, while not perfect, definitely simplifies complicated networking code. It has tons of wonderful features (of which this JSON conversion is probably one of the most minor features). It's definitely worth checking out. FYI, AFNetworking 2.0 only supports iOS 6 and higher (use AFNetworking 1.x if you need support for iOS 5). – Rob Jan 16 '14 at 04:45
  • @Rob I was asking about AFNetworking 2.0, I already have implemented AFNetworking 1.x. All my client app's have been updated for iOS7. – Pavan Jan 16 '14 at 04:54
  • Ah. AFNetworking 1.x did the JSON conversion for you, too. Regardless, the improvements as you go from 1.x to 2.0 is less dramatic, so I'm not sure you need to run out and immediately refactor your code. Perhaps check out the [AFNetworking 2.0 Migration Guide](https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-2.0-Migration-Guide). – Rob Jan 16 '14 at 04:57
  • LOL it does the conversion for me?! then WHY oh WHY did the previous client developer use SBJson to convert the dictionaries in the first project I worked on! Thats the main reason why I continued that same methodology for the projects that followed! *sighs* I shall remember your point for my next project! Cheers Rob – Pavan Jan 16 '14 at 04:59
  • If you want to do this in 1.x, check out the `parameterEncoding` property of `AFHTTPClient`. Maybe your previous developer didn't use `AFHTTPClient` (sometimes people just created operations without using that class). Anyway, if you can abandon iOS 5.0 support, AFNetworking 2.0 might be worth considering for future projects. Good luck and happy coding. – Rob Jan 16 '14 at 05:07