我正在尝试使用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)