アプリにjsonを実装するのは初めてです。
私のアプリでは、アプリからWebサービスにパラメーターを渡す必要があり、それに従ってWebサービスから応答を取得できますが、 ios5でアプリからパラメーターを渡すことができるチュートリアルを見つけることができません
私にとって役立つチュートリアルを入手できるネット上のチュートリアルはありますか。
グーグルを使って見つけようとしましたが、うまくいきませんでした。
次の手順に従って、json をプロジェクトに統合します。
1) 以下のファイルをコピーしてプロジェクトに貼り付けます。
2) 次のファイルを含む新しいグループ名をヘルプとして作成します。
3) 定数として名前が付けられているサポート ファイルに新しいファイルを作成します。
#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) 上記の変数 link where is your web service exist を変更します。これは、web サービスの URL を意味する「WEB_SERVICE_URL」です。</p>
5) 「Rest.h」ファイルを開き、1 つのメソッドを作成します。
-(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 ファイルの「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];
}