【发布时间】:2012-03-18 13:34:56
【问题描述】:
我是 AFNetworking 框架的新手。我尝试向服务器发送 JSON 请求以获取 JSON 响应,所以我尝试了这样的操作:
NSURL *url = [NSURL URLWithString:@"http://data.mycity.gov/api/views/INLINE/rows.json?method=index"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:[NSString stringWithFormat:@"application/json"] forHTTPHeaderField:@"Content-Type"];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success");
} failure:^(NSURLRequest* req,NSHTTPURLResponse *req2, NSError *error,id mex) {
NSLog(@"Failed");
NSLog(@"%@",[error description]);
}];
[operation start];
所以我总是发现自己进入了失败块,并带有以下错误描述:
Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x7b58030 {NSErrorFailingURLKey=http://data.mycity.gov/api/views/INLINE/rows.json?method=index, NSLocalizedDescription=Expected status code in (200-299), got 400}
有什么想法吗?我错过了什么吗?
【问题讨论】:
-
Malek,我不确定 JSON 请求是什么意思 - 这是一个 HTTP 请求,可能缺少一些请求的参数。您是否有从 API 请求数据的规范?
-
你好 Shai,是的,这是一个 HTTP 请求,但为了获得 JSON 响应。
标签: objective-c ios json afnetworking