NSString *str = @"http://m.weather.com.cn/data/101010100.html";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:str parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"----success----");
NSLog(@"JSON爲%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"----failure----");
NSLog(@"Error:%@",error);
}];
報錯信息:
AFNetworing_test[40154:60b] Error:Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8d2a020 {NSErrorFailingURLKey=http://m.weather.com.cn/data/101010100.html,
加上:
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
加上這個 錯誤解決
參照:http://stackoverflow.com/questions/19114623/request-failed-unacceptable-content-type-text-html-using-afnetworking-2-0
http://www.cocoachina.com/bbs/simple/?t176000.html
https://github.com/AFNetworking/AFNetworking
AFHTTPRequestOperation使用
-(void)AFHttpRequestOperationMethod
{
NSString *str = @"http://m.weather.com.cn/data/101010100.html";
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc]initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success");
NSLog(@"responseObject = %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error = %@",error);
}];
[[NSOperationQueue mainQueue] addOperation:op];
}