or chen
or chen

Reputation: 1

How to use json in phones applications with server side?

I am developing a new appliction for android and ios this month. I need to builld server side which communicate with the apps. I have read that the most popular way to communicate is to use json. My question is how?To send it with https?Can you please give me an a real example from the real life how it works? It is secured and safe? thank you!

Upvotes: 0

Views: 237

Answers (3)

Lithu T.V
Lithu T.V

Reputation: 20021

Multiple options there.

iOS

  1. NSJSONSerialization
  2. SBJson
  3. RestKit if you are using REST based apis

Android

check this answer

Upvotes: 1

Dilip Manek
Dilip Manek

Reputation: 9143

You have to create webservices to get data from server and use that web services in iphone ,for that you can use this code.it will geting data from webservice url which is in json format.you need to install sbjson library to parse json data in your project.

#import "SBJson.h"

- (void)viewDidLoad
{       

idArray = [[NSMutableArray alloc] init];

 NSString *post =@"";

                NSURL *url=[NSURL URLWithString:@"http://blog.bigwavemedia.co.uk/?json=get_recent_posts"];

                NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

                NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
                [request setURL:url];
                [request setHTTPMethod:@"POST"];
                [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
                //[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
                // [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                [request setHTTPBody:postData];

                //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

                NSError *error = [[NSError alloc] init];
                NSHTTPURLResponse *response = nil;
                NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];



                //NSLog(@"Response code: %d", [response statusCode]);
                if ([response statusCode] >=200 && [response statusCode] <300)
                {
                    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                    // NSLog(@"Response ==> %@", responseData);                    

                    SBJsonParser *jsonParser = [SBJsonParser new];
                    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];


                    //NSLog(@"json data is : %@",jsonData);

                    NSArray *allDataArray = [[NSArray alloc]init];
                    allDataArray= [jsonData objectForKey:@"posts"];


                    for(int i=0;i<[allDataArray count];i++)
                    {
                        NSDictionary *tempDictionary = [allDataArray objectAtIndex:i];


                        if([tempDictionary objectForKey:@"id"]!=nil)
                        {

                            [idArray addObject:[tempDictionary objectForKey:@"id"]];
                        }

                    }

                } else {
                    if (error) NSLog(@"Error: %@", error);
                    [self alertStatus:@"Connection Failed" :@"Does not find any data!"];
                }


            }
            @catch (NSException * e) {
                NSLog(@"Exception: %@", e);
                [self alertStatus:@"Data not found." :@"Does not find any data!"];
            }
[super viewDidLoad];
}

Here post variable is used to send argument in your url. And NSMutableURLRequest used to call the url and get data in json format than here we use SBJsonParser from sbjson library and parse that data in either dictionary or in array.

Upvotes: 1

nsgulliver
nsgulliver

Reputation: 12671

NSJSONSerialization is the way to go for you in case of iOS

Upvotes: 1

Related Questions