2012-04-19 37 views
4

我第一次在我的应用程序中实现json。iPhone:json的教程在ios 5

,并在我的应用我有传递参数从我的应用程序的web服务,并根据我可以从Web服务响应,但我没能找到,我可以从我的应用程序中传递参数的任何教程ios5

所以有没有在网上的任何教程,我可以从中获得对我有用的教程。

我试图找到使用谷歌,但我没有成功。

+0

退房本教程 - http://www.raywenderlich.com/5492/working-with-json-in-ios-5 – rishi 2012-04-19 05:31:39

+0

@RIP:我已检查过本教程,但我不知道如何以及在哪里传递参数。在本教程中,只有url是从应用程序传入的。 – 2012-04-19 05:33:46

回答

2

以下步骤将json集成到我们的项目中。

1)将以下文件复制粘贴到您的项目中。

2)创建新的组名称作为包含以下文件的帮助。

3)创建新的文件到支持文件是名作为constant.h

#define WEB_SERVICE_URL @"Your server url" //@"http://demo.google.com/webservice/" 

#define USERNAME @"Your user id"//static username to send everytime 
#define PASSWORD @"Your Password"//static password to send everytime 

4)改变的变量链接,为您的Web服务存在以上,这意味着你的web服务的URL“WEB_SERVICE_URL “

5)打开”Rest.h“文件并创建一个方法。

-(void)Get_StoreDetails:(SEL)seletor andHandler:(NSObject*)handler andCountryId:(NSString *)CountryCode; 

6)打开“Rest.m”文件和哪个参数传递给服务器的方法的代码。

-(void)Get_StoreDetails:(SEL)seletor andHandler:(NSObject *)handler andCountryId:(NSString *)country_id{ 
    NSDictionary *req = [NSDictionary dictionaryWithObjectsAndKeys:@"content/get_stores",@"request", 
         [NSDictionary dictionaryWithObjectsAndKeys:USERNAME,@"username", 
          PASSWORD,@"password", 
          country_id,@"country_id", 
          nil],@"para",nil]; 
    [[[JSONParser alloc] initWithRequest:req sel:seletor andHandler:handler] autorelease]; 

} 

7)现在Rest应用程序已准备好发送请求,然后现在这个方法调用我们的视图控制器。投入“viewWillAppear中”方法在.h文件中

#import <UIKit/UIKit.h> 

@interface JsonDemoAppViewController : UIViewController{ 
    UIAlertView *alertCtr; 
} 

@property(nonatomic,retain)NSMutableArray *arrForStores; 
- (void)getData:(id)object; 
@end 

下面的代码

下面的代码放入在.m文件并导入“Rest.h”中的“viewWillAppear中”的方法。

- (void)viewWillAppear:(BOOL)animated { 
    self.arrForStores=nil; 
    //============================Json Initialization=================================================== 
    REST *rest = [[REST alloc] init]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [rest Get_StoreDetails:@selector(getData:) andHandler:self andCountryId:@"12"]; 

    alertCtr = [[[UIAlertView alloc] initWithTitle:@"\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; 
    [alertCtr show]; 

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 

    // Adjust the indicator so it is up a few pixels from the bottom of the alert 
    indicator.center = CGPointMake(alertCtr.bounds.size.width/2, alertCtr.bounds.size.height - 50); 
    [indicator startAnimating]; 
    [alertCtr addSubview:indicator]; 
    [indicator release]; 
    //=================================================================================================== 

} 

然后答复进入服务器的时间对象存储到变量,我们创建metod调用的时间。

- (void)getData:(id)object{ 
    NSLog(@"%@",object); 
    if ([object isKindOfClass:[NSDictionary class]]) { 
     NSLog(@"Controll comes here.."); 
     //IF your response type is NSDictionary then this code to run 
    } 

    if ([object isKindOfClass:[NSArray class]]) 
    { 
     self.arrForStores=[NSArray arrayWithArray:(NSArray*)object]; 
     //IF your response type is NSArray then this code to run 
    } 

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
    [alertCtr dismissWithClickedButtonIndex:0 animated:YES]; 

} 

Download the source code From Here