Grails側にJSONデータを返すアクションを用意します。
- def json = {
- def jsonData=["任天堂":"wii","sony":"PlayStation3","Microsoft":"XBOX360"]
- render(text:jsonData as JSON,contentType:"application/x-json")
- }
contentTypeは application/json でも無指定でも読み込める。
Objective-C側
- NSURL *jsonURL = [NSURL URLWithString:@"http://localhost:8080/jsonSample/test/json"];
- NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL encoding:NSUTF8StringEncoding error:nil];
- if (jsonData == nil) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Webservice Error" message:@"JSON Dataの取得に失敗しました。" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
- [alert show];
- [alert release];
- } else {
- SBJSON *json = [SBJSON new];
- json.humanReadable = YES;
- id jsonItem = [jsonData JSONValue];
- NSLog([json stringWithObject:jsonItem error:NULL]);
- }
NSString の initWithContentsOfURL で encoding を指定するのがポイントです。
encodingがあってないと nil になって通信出来てないのかと思って悩んでました・・・
そろそろインターフェース側の処理をやっていこうかな :)
0 件のコメント:
コメントを投稿