我是AFNetworking
框架的新手。我嘗試發送JSON請求到服務器,以獲得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? – 2012-03-18 13:52:20
你好Shai,是的,這是一個HTTP請求,但爲了獲得JSON響應。 – Luca 2012-03-18 14:23:37