Skip to Content
Author's profile photo Yijeng Liu

Providing/consuming JSON to/from HANA in iPhone using Xcode

Mobile devices are able to communicate to HANA via XS engine.  The protocol is HTTP andthe data can be passed in JSON.  This is the counter part of the other blog(http://scn.sap.com/community/developer-center/hana/blog/2013/05/30/read-write-json-api-from-hana).

HTTP request from iPhone:

NSString *serverAddress = [NSString stringWithFormat:@”http://<host>:<port>/packagepath/<file>.xsjs?parameter =%@“, object];
NSString *userName = @”user”;
NSString *password = @”pass”;
NSString *loginString = [NSString stringWithFormat:@%@:%@, userName, password];
// “Base64 encode” is a class method you need to create by yourself or search the web
NSString *encodedLoginData = [Base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];
NSString *authHeader = [@”Basic ” stringByAppendingFormat:@”%@”, encodedLoginData];

NSOperationQueue*queue = [[NSOperationQueue alloc] init];
NSMutableURLRequest*request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverAddress] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];

[request setHTTPMethod: @”POST”];

[request setValue:@”application/x-www-form-urlencoded” forHTTPHeaderField:@”content-type”];

[request addValue:authHeader forHTTPHeaderField:@”Authorization”];

NSArray* arrayobject4name = [NSArray arrayWithObjects:@”name11″,@”name12″, nil];

NSArray* arrayobject4value = [NSArray arrayWithObjects:@”value11″,@”value12″, nil];

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:

     @”value1″, @”name1″,

     @”value2″, @”name2″,

     arrayobject4name,@”arrayobject4name”,

     arrayobject4value,@”arrayobject4value”,nil];

NSError* error;

NSData* jsondata = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

NSString* jsonbody = [[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding];

NSData* rData = [NSData dataWithBytes:[jsonbody UTF8String] length:[jsonbody length]];
[request setHTTPBody:rData];

NSError* requestError;

NSURLResponse* urlResponse = nil;

NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse*urlResponse, NSData *responseData, NSError
*requestError) {

                 if ([responseData length] > 0 && requestError == nil){ //do something for responseData}

                 else if ([responseData length] == 0 && requestError == nil){ //do something}

                  else if (requestError != nil){//error handling}

}];

HTTP response parsing in iPhone:

NSError* convertError;

NSDictionary* jsonDict = [NSJSONSerialization JSONObjectWithData:responsedata options:NSJSONReadingAllowFragments error:&convertError];

if (convertError != nil){ //do something}

else{

          NSArray* jsonArray = [jsonDict objectForKey:@”entry”];

          NSMutableArray* _objects = [[NSMutableArray alloc]initWithCapacity:[jsonArray count]];

          for (NSArray* item in jsonArray){

               for (NSArray* obj in item){

                    if ([[obj objectAtIndex:0] isEqual: @”name1″]){ //do something}

                    else if ([[obj objectAtIndex:0] isEqual: @”name2″]){ //do something}

                    else if ([[obj objectAtIndex:0] isEqual:@”arrayname1″]){ //do something}

                    else if ([[obj objectAtIndex:0] isEqual: @”arrayname2″]){ //do something}

               }

          }

}

Please reference the other blog(http://scn.sap.com/community/developer-center/hana/blog/2013/05/30/read-write-json-api-from-hana) for the format of JSON.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.