AFNetworing 2.0

来源:互联网 发布:欧陆风云4mac修改时间 编辑:程序博客网 时间:2024/05/16 16:55
1.get请求
 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 = [NSURLURLWithString:str];

   NSURLRequest *request = [NSURLRequestrequestWithURL:url];

    

    AFHTTPRequestOperation *op = [[AFHTTPRequestOperationalloc]initWithRequest:request];

    op.responseSerializer = [AFJSONResponseSerializerserializer];

    

    op.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];

    

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

       NSLog(@"success");

       NSLog(@"responseObject = %@",responseObject);

        

        

        

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

       NSLog(@"Error = %@",error);

    }];

    

    [[NSOperationQueuemainQueue] addOperation:op];


    

    

}




2. 
0 0
原创粉丝点击