次の問題が発生しました: iPhone アプリをサーバー上のデータベースに接続したいのですが。したがって、いくつかの (単純な) .php ファイルを使用して、DB へのアクセスを管理します。新しいデータの挿入はすでに機能していますが、フェッチしたデータを NSMutableArray に変換するのに問題があります。
NSURL *contentURL = [NSURL URLWithString:[kHOSTURL stringByAppendingString:kGETBarsURL]];
NSLog(@"URL : %@", contentURL);
NSData *contentData = [NSData dataWithContentsOfURL:contentURL];
NSLog(@"Data : %@", contentData);
NSError *e = nil;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:contentData
options:kNilOptions
error:&e];
NSLog(@"JSON : %@", jsonArray);
NSLog(@"Error : %@", e);
出力は次のようになります (I 'XX' と短縮された 'Data :'):
2012-04-28 13:49:37.229 XX[14434:f803] URL : http://xx/getBars.php
2012-04-28 13:49:37.389 XX[14434:f803] Data : <5b7b2275 6e697175 65223a22 34222c22 4e616d65 223a2254 65737422 2c224465 7461696c 73223a22 54686973 49734154 65737422 7d2c7b22 ...>
2012-04-28 13:49:37.390 XX[14434:f803] JSON : (null)
2012-04-28 13:49:37.392 XX[14434:f803] Error : Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x6daa610 {NSDebugDescription=Garbage at end.}
ブラウザでページを開くと、次のように表示されます。
[{"unique":"4","Name":"Test","Details":"ThisIsATest"},
{"unique":"5","Name":"Test","Details":"ThisIsATest"},
{"unique":"6","Name":"Test","Details":"ThisIsATest"},
{"unique":"7","Name":"Test","Details":"ThisIsATest"},
{"unique":"8","Name":"Test","Details":"ThisIsATest"},
{"unique":"9","Name":"Test","Details":"ThisIsATest"},
{"unique":"10","Name":"Test","Details":"ThisIsATest"}]
NSJSONSerialization で他のオプションも試しましたが、うまくいきませんでした:(ここで誰か助けてもらえますか?
2012-04-28 14:18:30.192 XX[14541:f803] Encoding : [{"unique":"4","Name":"Test","Details":"ThisIsATest"},{"unique":"5","Name":"Test","Details":"ThisIsATest"},{"unique":"6","Name":"Test","Details":"ThisIsATest"},{"unique":"7","Name":"Test","Details":"ThisIsATest"},{"unique":"8","Name":"Test","Details":"ThisIsATest"},{"unique":"9","Name":"Test","Details":"ThisIsATest"},{"unique":"10","Name":"Test","Details":"ThisIsATest"}]
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16106315-6']);
_gaq.push(['_setDomainName', '.xx.de']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>