25 Eylül 2012 Salı

Datasnap rest sunucusu ile iletişim(iOS Client)

Daha önce TDataset class'ına uzanım fonksiyonu olarak yazdığım ToJSONData isminde bir fonksiyonum vardı.Bu fonksiyon sayesinde veritabanında ki verileri kolayca json formatına çevirip dışarı sunabiliyordum.

Bu servislere de iOS ortamından erişmeyi görücez.

Person.h

@interface Person : NSObject
    @property(nonatomic) NSInteger rid;
    @property(nonatomic,retain) NSString *firstname;
    @property(nonatomic,retain) NSString *lastname;
@end

Person.m

#import "Person.h"

@implementation Person
  @synthesize rid;
  @synthesize firstname;
  @synthesize lastname;
@end

mainViewController.m

#import "mainViewController.h"
#import "Person.h"
#import "SBJson.h"

@interface mainViewController ()
  
@end

Person *person;
NSMutableArray * personlist;

@implementation mainViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
               
    personlist=[NSMutableArray new]; 
    NSURL *url = [[NSURL alloc] 
                  initWithString:@"http://192.168.1.7:8080/datasnap/rest/TServerMethods1/GetPersonList"];
    NSError *error = nil;
    NSStringEncoding encoding;
    NSString *rawjson = [[NSString alloc] initWithContentsOfURL:url
                                                   usedEncoding:&encoding 
                                                          error:&error];
    //formatting : [NSString stringWithFormat:@"%d", rawjson.length];
    if (rawjson.length!=0){
        SBJsonParser * parser=[SBJsonParser new];  
        NSDictionary *jsonObject = [parser objectWithString:rawjson error:&error];
    
        NSArray *mainlist = [jsonObject objectForKey:@"result"];
        NSArray *innerlist= [mainlist objectAtIndex:0];
        
        for (NSDictionary *item in innerlist) {
            person =[Person new];
            person.rid=[[item objectForKey:@"rid"] integerValue];
            person.firstname=[item objectForKey: @"firstname"]; 
            person.lastname=[item objectForKey: @"lastname"]; 
            [personlist addObject:person]; 
        }
    }
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsneSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 //warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [personlist count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    Person * personItem=[personlist objectAtIndex:indexPath.row]; 
    cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",
                         [personItem firstname],[personItem lastname]];
    return cell;
}




Demo :





Kaynaklar :

http://json.org/
http://stig.github.com/json-framework/
http://docwiki.embarcadero.com/RADStudio/en/DataSnap_REST_Messaging_Protocol
http://jasarien.com/?p=428
http://stackoverflow.com/questions/4919249/how-to-parse-json-into-objective-c-sbjson
http://stackoverflow.com/questions/3995519/how-to-use-stringwithcontentsofurlencodingerror
http://stackoverflow.com/questions/12567491/how-to-parse-this-json-data-with-objective-c-sbjsonparser

Hadi hayırlı işler. :)

4 yorum:

  1. Ellerine sağlık İsmail çok güzeldi

    YanıtlaSil
  2. Teşekkür ederim Mehmet hocam.
    Bu konuda ki çalışmalarım yoğun bir şekilde devam ediyor.
    Zaman buldukça paylaşmaya çalışıcam. :)

    YanıtlaSil
  3. İsmail Kardeş.. Ellerine ve Emeğine Sağlık Çok Güzel Bir Çalışma Olmuş...

    YanıtlaSil
  4. Bu yorum yazar tarafından silindi.

    YanıtlaSil