sys_debug
sys_debug

Reputation: 4003

json parsing into dictionary ios

I have the following script in PHP:

<?php $sender = $_POST['sender'];
      $rcpt = $_POST['rcpt'];
      $message = $_POST['message'];

      $someArray = array("Bannana", "Apple", "SomeCheese");
      print_r($someArray);

      echo json_encode($someArray);

?>

and the following button action:

NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://development.com/ios/responseScript.php" ] ];

/**********Set Request properties*************/
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];


NSURLResponse *response;
NSError *err;

NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSUInteger content = [NSString stringWithUTF8String:[returnedDictionary count]];
//NSLog(@"responseData: %@", content);
self.responseLabel.text = [NSString stringWithFormat:@"Count:    %u", content];

What I am trying to achieve is that the json being echoed should come back in the form of a response and be printed to the screen. At best I got that the return was null and am sure it is not. I have a working example but using array instead of dictionary, and the outcome is attached below:

NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://development.com/ios/responseScript.php" ] ];
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
//NSLog(@"responseData: %@", content);
self.responseLabel.text = [NSString stringWithFormat:@"Data:    %@", content];

Outcome:

2013-01-30 19:07:07.919 testResponse[15921:c07] responseData: Array ( [0] => Bannana [1] => Apple [2] => SomeCheese ) ["Bannana","Apple","SomeCheese"]

Please help? Thank you a lot

Upvotes: 0

Views: 347

Answers (2)

Jim
Jim

Reputation: 73936

NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

You are getting an NSData object here, not an NSDictionary object.

NSUInteger content = [NSString stringWithUTF8String:[returnedDictionary count]];

You are getting the number of items in the (nonexistent) dictionary here, not its contents.

Given that you are updating user interface elements in this code, I am assuming it's being run on the main thread. You shouldn't be making synchronous requests on the main thread, that will just make your application hang while it's waiting on the network. Either make synchronous requests on a background thread or make asynchronous requests on the main thread.

From there, look at the documentation for the classes you are using and pay attention to the warnings Xcode is surely giving you.

Upvotes: 0

Stavash
Stavash

Reputation: 14304

There's a problem with this line:

NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

sendSynchronousRequest:returningResponse:error: returns NSData, not NSDictionary.

Read the documentation, be sure you know what types you're dealing with and good luck.

Upvotes: 1

Related Questions