小编Goe*_*ran的帖子

使用AFNetworking检索JSON的问题

我正在尝试使用AFNetworking检索一些JSON数据.

我的服务器组件(PHP/ZendFramework)提供以下响应:

>curl -i http://test.local:8080/public/appapi/testaction/format/json
HTTP/1.1 200 OK
Date: Mon, 06 Feb 2012 10:37:20 GMT
Server: Apache
X-Powered-By: PHP/5.3.8
Content-Length: 35
Content-Type: application/json
{"entries":"test","abcxxx":"test2"}
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码来检索此json数据:

NSString *baseurl = @"http://test.local:8080";

NSString *path = @"/public/appapi/testaction/format/json";

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"];

[client getPath:path parameters:nil success:^(__unused 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]);
}];
Run Code Online (Sandbox Code Playgroud)

在模拟器中运行时,控制台显示以下输出:

2012-02-06 11:22:39.800 …
Run Code Online (Sandbox Code Playgroud)

json afnetworking

19
推荐指数
3
解决办法
1万
查看次数

标签 统计

afnetworking ×1

json ×1