无法解析JSON响应中的响应

时间:2016-01-21 07:58:40

标签: afnetworking-3

我正在使用AFNetworking 3,Xcode 7.2 for iOS 9。

AFURLSessionManager *ses = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
ses.responseSerializer = [AFJSONResponseSerializer serializer];

//Request
[[AFJSONRequestSerializer serializer] setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"GET" URLString:endPoint parameters:parameters error:nil];

NSURLSessionDataTask *dataTask = [ses dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];
[dataTask resume];

但我收到错误回复:

  

Domain = com.alamofire.error.serialization.response Code = -1016"请求失败:不可接受的内容类型:text / plain"

请求内容类型仍设置为" text / plain" 我不确定我错过了什么。

1 个答案:

答案 0 :(得分:1)

我自己使用了http响应序列化器,它工作正常。

AFURLSessionManager *ses = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    ses.responseSerializer = [AFHTTPResponseSerializer serializer];

NSMutableDictionary * innerJson = [NSJSONSerialization
                                               JSONObjectWithData:responseObject options:kNilOptions error:&error1];