Timmy
Timmy

Reputation: 107

Parse JSON with iOS 6 and xCode 4.5.2

I'm new in objective-c and and building a sign in page for a specific website. When the user provide information the following JSON return back from the server:

  {
   "object":{},
   "resultCode":0,
   "resultMessage":"You successfully signed in "
 }

And this what I'm trying to do based on a tutorial I found:

-(IBAction)login{
        // Download JSON

    NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"https://www.example.com/login?username=%@&password=%@", nationalID.text,passowrd.text]]];
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

        NSArray *response = [json objectForKey:@"resultMessage"];
        NSLog(@"response %@",response); 
}

I want to log out "resultMessage" but somehow it throws an exception:

    2012-12-13 06:46:50.607 myProject[7178:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1c8cdeb 0xbff817 0x2f1a 0x10de705 0x15920 0x158b8 0xd6671 0xd6bcf 0xd5d38 0x4533f 0x45552 0x233aa 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x1be77e3 0x1be7668 0x1265c 0x279d 0x26c5)
libc++abi.dylib: terminate called throwing an exception

Upvotes: 0

Views: 3378

Answers (4)

Mehmood
Mehmood

Reputation: 931

First Try static url like:

https://www.example.com/login?username=admin&password=123

if this returns you correct response then try Property Variables like:

https://www.example.com/login?username=self.username.text&password=self.password.text

And one mistake you have made is that resultMessage is string not an Array.

Hope this helps you

Upvotes: 0

Jacob Dyer
Jacob Dyer

Reputation: 1

Looks like you have a simple typo, you are using:

nationalID.text,passowrd.text

You probably meant to not misspell 'password'.

Upvotes: 0

NSResponder
NSResponder

Reputation: 16861

Looks like the -dataWithContentsOfURL: message failed and returned nil, which is exactly what the error message says. You need to confirm that your -stringWithFormat: message is giving you the correct URL format, and check whether the server is returning what you expect.

Upvotes: 2

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

if you could observe the issue description:

'NSInvalidArgumentException', reason: 'data parameter is nil'

means the response from your response from the web url is nil or NULL
this is not a issue related to JSON parser.

Upvotes: 0

Related Questions