2

我是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}

有任何想法吗?我错过了什么吗?

4

2 回答 2

1

状态码 400 表示 - 错误请求 由于语法错误,服务器无法理解请求。客户端不应该在没有修改的情况下重复请求。

当我尝试访问该 URL: http://data.mycity.gov/api/views/INLINE/rows.json?method=index " 时,出现未找到错误

确保您使用的 API 正确,如果 Web 服务返回 JSON,您应该能够在浏览器中键入该 URL 并获得 JSON 响应。

于 2012-03-18T14:14:06.300 回答
0

您的服务器似乎返回 400,“错误请求”。检查您的网址是否正确。

顺便说一句,你stringWithFormat是多余的。你可以更换

[NSString stringWithFormat:@"application/json"] 

@"application/json"
于 2012-03-18T14:09:11.587 回答