AFNetworking ライブラリを使用して、iPhone アプリ内に単純な JSON を読み込もうとしています。私は2つの異なるアプローチを使用しています:
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/test.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Name: %@", [JSON valueForKeyPath:@"name"]);
} failure:^(NSURLRequest* req,NSHTTPURLResponse *req2, NSError *error,id mex) {
NSLog(@"%@", [error description]);
}];
[operation start];
そして、AFHttpClient を使用します (これは、REST API を処理するためのすべての機能を備えているため、私のお気に入りです。
NSString *baseurl = @"http://www.mysite.com";
NSString *path = @"/test.json";
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
//[client setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"];
[client getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
//NSLog(@"sjson: %@", [JSON valueForKeyPath:@"entries"]);
NSLog(@"sjson: %@", JSON);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error Code: %i - %@",[error code], [error localizedDescription]);
}];
さて、問題: どちらの場合も、空の Json を受け取っています。サーバー側をテストしたところ、Web サービスは適切な応答と適切な MIME タイプ (application/json) を提供しています。
このディスカッションhttps://github.com/AFNetworking/AFNetworking/issues/175を既に確認しまし たが、私のターゲットは 5.0 を指しており、AFNetworking ライブラリに対して ARC が有効になっていません。
ここで明らかな何かが欠けているのかもしれませんが、現時点では修正できません。
どうもありがとう!
クロース