使用 Afnetworking 从服务器获取数据时出错

error while getting data from server using Afnetworking

我想从以 json 编码的服务器获取数据以显示它,所以我使用 afnetworking 库但我得到错误这是我的代码

  NSURL * url =[NSURL URLWithString:@"http://software-sultan.com/alaa/image_test.php"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // 3
    array = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];



} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // 4
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
    NSLog(@"%@",error);
}];

// 5
[operation start];

这里是错误

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7f920964bff0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f920965cc20> { URL: http://software-sultan.com/alaa/image_test.php } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 126781;
"Content-Type" = "text/html";
Date = "Thu, 29 Jan 2015 19:00:55 GMT";
"Keep-Alive" = "timeout=30";
Server = "Apache/2";
"X-Powered-By" = "PHP/5.3.13";

} }, NSErrorFailingURLKey=http://software-sultan.

看起来您正在尝试用 AFJSONResponseSerializer 解析 JSON 响应,但网络服务器发送 text/html 作为内容类型。 JSON 回复通常使用 Content-Type: application/json header.

此外,如果响应被成功解析为 JSON,作为完成块的第二个参数给出的 responseObject 已经是包含您的数据的有效 NSArray。因此不需要使用 NSJSONSerialization.

再次创建它