iOS網路-AFNetworking序列化

來源:互聯網
上載者:User

標籤:

AFN它內部預設把伺服器響應的資料當做json來進行解析,所以如果伺服器返回給我的不是JSON資料那麼請求報錯,這個時候需要設定AFN對響應資訊的解析方式。AFN提供了三種解析響應資訊的方式,分別是:AFJSONResponseSerializer---------JSON 預設模式 AFXMLParserResponseSerializer---XML
AFHTTPResponseSerializer---------二進位響應資料
還有一種情況就是伺服器返回給我們的資料格式不太一致(開發人員工具Content-Type:text/xml),那麼這種情況也有可能請求不成功。解決方案: 直接在原始碼中修改,添加相應的Content-Type
拿到這個屬性,添加到它的集合中
AFJSONResponseSerializer模式範例程式碼:
-(void)json{    //1.建立會話管理者    AFHTTPSessionManager* manager =  [AFHTTPSessionManager manager];    NSDictionary *dictM = @{                            @"username":@"520it",                            @"pwd":@"520it",                            @"type":@"JSON"                            };    //2.發送GET    [manager GET:@"http://120.25.226.186:32812/login" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        NSLog(@"成功--%@",responseObject);    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        NSLog(@"失敗--%@",error);    }];}

AFXMLParserResponseSerializer模式範例程式碼:

-(void)xml{    //1.建立會話管理者    AFHTTPSessionManager* manager =  [AFHTTPSessionManager manager];    NSDictionary *dictM = @{                            @"type":@"XML"                            };     manager.responseSerializer = [AFXMLParserResponseSerializer serializer];    //2.發送GET    [manager GET:@"http://120.25.226.186:32812/video" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSXMLParser *parser) {        //設定代理        parser.delegate = self;        //開始解析        [parser parse];    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        NSLog(@"失敗--%@",error);    }];}#pragma mark - NSXMLParserDelegate-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{    NSLog(@"%@---%@",elementName,attributeDict);}

AFHTTPResponseSerializer模式範例程式碼:

-(void)http{    //1.建立會話管理者    AFHTTPSessionManager* manager =  [AFHTTPSessionManager manager];    NSString *urlStr = @"http://img4q.duitang.com/uploads/item/201411/21/20141121231731_PkXNk.jpeg";    //調整AFN的解析方式//    manager.responseSerializer = [AFJSONRequestSerializer serializer];//預設    manager.responseSerializer = [AFHTTPResponseSerializer serializer];    //2.發送GET    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        NSLog(@"成功--%@",[responseObject class]);        NSLog(@"%@",[NSThread currentThread]);        self.imageView.image = [UIImage imageWithData:responseObject];    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        NSLog(@"失敗--%@",error);    }];}
-(void)http2{    //1.建立會話管理者    AFHTTPSessionManager* manager =  [AFHTTPSessionManager manager];    NSString *urlStr = @"http://www.baidu.com";    //調整AFN的解析方式 如果報錯(text/html是在報錯裡面提供的),百分之七八十都是因為缺少這倆句代碼原因    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html",缺少上面代碼報的錯誤 manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set.",缺少上面代碼報的錯誤 //2.發送GET [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"成功--%@",[responseObject class]); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"失敗--%@",error); }];}

 

iOS網路-AFNetworking序列化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.