首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone: ios 5中的json教程

iPhone: ios 5中的json教程
EN

Stack Overflow用户
提问于 2012-04-19 05:29:06
回答 1查看 1.5K关注 0票数 4

我是第一次在我的应用中实现json。

在我的应用程序中,我必须通过将参数从我的应用程序传递到webservice,根据这一点,我可以从web服务获得响应,但是我找不到任何教程,可以在ios5中从我的应用程序中传递参数。

因此,有任何教程在网上,我可以从教程,这是有用的我。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-19 05:37:35

接下来的步骤是将json集成到我们的项目中。

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

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

3)将新文件创建到支持文件中,该文件名为constant.h

代码语言:javascript
复制
#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服务存在的位置,这意味着您的webservice“WEB_SERVICE_URL”的url

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

代码语言:javascript
复制
-(void)Get_StoreDetails:(SEL)seletor andHandler:(NSObject*)handler andCountryId:(NSString *)CountryCode;

6)打开“Rest.m”文件,并将该方法传递给服务器的参数传递给代码。

代码语言:javascript
复制
-(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应用程序准备好发送请求,然后这个方法调用我们的视图控制器。

将以下代码放入.h文件中的“.h”方法

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface JsonDemoAppViewController : UIViewController{
    UIAlertView *alertCtr;
}

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

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

代码语言:javascript
复制
- (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。

代码语言:javascript
复制
- (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];

}

从这里下载源代码

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10222167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档