gpt4 book ai didi

iPhone:ios 5 中的 json 教程

转载 作者:行者123 更新时间:2023-12-03 19:18:23 24 4
gpt4 key购买 nike

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

在我的应用程序中,我必须传递参数从我的应用程序到网络服务,据此我可以从网络服务获得响应,但我找不到任何可以传递参数的教程来 self 在 ios5

中的应用程序

网上有没有什么教程可以让我得到对我有用的教程。

我尝试使用谷歌查找,但没有成功。

最佳答案

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

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

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

3)在Support Files中创建新文件,名称为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_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 应用程序已准备好发送请求,然后该方法调用我们的 View Controller 。

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

#import <UIKit/UIKit.h>

@interface JsonDemoAppViewController : UIViewController{
UIAlertView *alertCtr;
}

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

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

- (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];
//===================================================================================================

}

然后回复进入服务器,此时对象存储到我们创建调用方法的变量中。

- (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

关于iPhone:ios 5 中的 json 教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10222167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com