我想通过Afnetworking从服务器获取json数据,但是我收到了错误。
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
manager.requestSerializer = [[AFJSONRequestSerializer alloc] init];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:url parameters:0 success:^ void(AFHTTPRequestOperation * operationManager, id result)
{
NSLog(@"My Result Is :%@", result);
} failure:^ void(AFHTTPRequestOperation * operationManger, NSError * error)
{
NSLog(@"Error :%@", error.localizedDescription);
}];
答案 0 :(得分:0)
if ([[AFNetworkReachabilityManager sharedManager] isReachable]) {
//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
operation.responseSerializer = [AFHTTPResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id _responseObject) {
id responseObject = [ NSJSONSerialization JSONObjectWithData : _responseObject
options : kNilOptions
error : nil ] ;
NSLog(@"Response:%@",responseObject);
// Success ;
if( _success )
{
_success( responseObject ) ;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
if( _failure )
{
_failure( error) ;
}
}];
//call start on your request operation
[operation start];
}
else
{
[Utility showInterNetConnectionMessage];
NSError *error;
if( _failure )
{
_failure( error) ;
}
}