我是第一次在我的应用中实现json。
在我的应用程序中,我必须通过将参数从我的应用程序传递到webservice,根据这一点,我可以从web服务获得响应,但是我找不到任何教程,可以在ios5中从我的应用程序中传递参数。
因此,有任何教程在网上,我可以从教程,这是有用的我。
我试图找到使用谷歌,但我没有成功。
发布于 2012-04-19 05:37:35
接下来的步骤是将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 everytime4)更改上面的变量链接,即您的web服务存在的位置,这意味着您的webservice“WEB_SERVICE_URL”的url
5)打开“Rest.h”文件并创建one方法。
-(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应用程序准备好发送请求,然后这个方法调用我们的视图控制器。
将以下代码放入.h文件中的“.h”方法
#import <UIKit/UIKit.h>
@interface JsonDemoAppViewController : UIViewController{
UIAlertView *alertCtr;
}
@property(nonatomic,retain)NSMutableArray *arrForStores;
- (void)getData:(id)object;
@end下面的代码放入.m文件中的“.m”方法并导入“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];
//===================================================================================================
}然后回复进入服务器,那个时候,对象存储在变量中,然后我们创建调用的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];
}从这里下载源代码
https://stackoverflow.com/questions/10222167
复制相似问题