当我们向后台请求得到一个html网页时,后台返回的数据如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
<meta HTTP-EQUIV="expires" CONTENT="0">
<title>demo</title>
<link rel="stylesheet" href="https://cdn1.huifintech.com/font-admin/plugin/weui/weui.min.css">
<link rel="stylesheet" href="https://cdn1.huifintech.com/font-admin/plugin/weui/jquery-weui.min.css">
<style>
.weui-flex {
padding: 15px;
}
.appsc img {
width: 100%;
height: 100%;
}
.weui-flex h3 {
color: #333;
font-size: 0.85rem;
}
.weui-flex h5 {
color: #999;
font-size: 0.55rem;
padding: 0.3rem 0;
}
.weui-flex p {
color: #666;
font-size: 0.55rem;
text-indent: 1rem;
overflow: hidden;
letter-spacing: 0.08rem;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 7;
-webkit-box-orient: vertical;
}
.weui-flex .content {
margin-top: 30%;
padding-left: 0.8rem;
}
.detail {
background-color: #ffd5d1;
font-size: 12px;
margin: 0.8rem;
border-radius: 5px;
padding: 0.5rem;
padding-right: 0;
}
.detail .weui-flex {
padding: 0;
}
</style>
</head>
<body>
<div class="weui-flex">
<div class="weui-flex__item appsc"><img src="http://192.168.1.133:8087//hxxentry_shop/images/shop/appsc1.png" alt=""></div>
<div class="weui-flex__item">
<div class="content">
<h3>政法《金融管理》直本名校通关版</h3>
<h5>课程简介</h5>
<p>双方的首发肯定定是都是浪费家里都是荆防颗粒的设计费 都放假了点击点击似而非发肯定定是都是浪费家里都是荆防颗粒的设计费 都放假了点击点击似而发肯定定是都是浪费家里都是荆防颗粒的设计费
都放假了点击点击似而发肯定定是都是浪费家里都是荆防颗粒的设计费 都放假了点击点击似而都是肯定是都是浪费家里都是荆防颗粒的设计费 都放假了点击点击似而非</p>
</div>
</div>
</div>
<div class="detail">
<div class="weui-flex">
<div class="weui-flex__item" >开课时间:</div>
<div class="weui-flex__item">总课时:</div>
</div>
<div class="weui-flex" style="padding-top: 8px">
<div class="weui-flex__item">就读机构:上海新康进修学院(黄浦校区)</div>
</div>
<div class="weui-flex" style="padding-top: 8px">
<div class="weui-flex__item">上课地址:上海市黄浦区西藏中路18号港陆广场1505</div>
</div>
</div>
</body>
</html>
此时 我们会发现发生了错误
这是因为 AFNetworking默认把响应结果当成json来处理,(默认manager.responseSerializer = [AFJSONResponseSerializer serializer]) ,很显然,我们请求的 返回的并不是一个json文本,而是一个html网页,但是AFNetworking并不知道,它坚信请求的结果就是一个json文本!然后固执地以json的形式去解析,显然没办法把一个网页解析成一个字典或者数组,所以产生了上述错误.
然而,我们期望它能够正确地处理这个情形,而不是提示一个错误.
这时候 你必须告诉AFNetworking:别把这个网页当json来处理!
此时需要设置一下参数,代码如下:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"text/plain",nil];
[manager POST:@"http://192.168.1.133:8087/hxxentry_shop/shop/course/getCourseDetailHtml" parameters: parameter progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
此时便可得到一个字符串,这个字符串里就是我们要的html数据
通过webview展示即可:
[_webView loadHTMLString:htmlStr baseURL:nil];
网友评论