使用AFNetworking实现POST和GET请求时遇到的问题

问题1.  

 错误日志:

Error: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type:text/html" UserInfo=0x7f8e5868c2c0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f8e5845a310> { URL: http://cyzaobao.cn/wep_app/wep_comein/index } { status code: 200, headers {

    Connection = "Keep-Alive";

    "Content-Length" = 6431;

    "Content-Type" = "text/html";

    Date = "Thu, 22 Jan 2015 09:26:05 GMT";

    "Keep-Alive" = "timeout=5, max=100";

    Server = Apache;

} },NSErrorFailingURLKey=http://cyzaobao.cn/wep_app/wep_comein/index, com.alamofire.serialization.response.error.data=<.......(省略)


错误原因:AFNetworking  默认不支持text/html

错误解决办法:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    NSDictionary *parameters = @{@"action": @"2",@"page": @"1"};
    [manager POST:kPostURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON:%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

注意:检查下你是不是漏写了红色标记的这句话

问题2:
错误原因:

manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObjects:@"text/html", nil];

注意:上面绿色部分是不是漏写了


问题3:

 Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unescaped control character around character 4426.) UserInfo=0x7fc5e8f6f170 {NSDebugDescription=Unescaped control character around character 4426.}


这种错误可以打印出错误信息:

<span style="font-size:12px;">AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    NSDictionary *parameters = @{@"action": @"1",@"page": @"1"};
    [manager POST:kPostURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {</span>
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;"><span style="color: #587ea8">NSLog</span>(<span style="color: #e82300">@"JSON:%@"</span>,responseObject);</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; min-height: 16px;"><span style="font-size:12px;">        </span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; min-height: 16px;"><span style="font-size:12px;">        </span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">    } <span style="color: #587ea8">failure</span>:^(<span style="color: #c35900">AFHTTPRequestOperation</span> *operation, <span style="color: #c35900">NSError</span> *error) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">        <span style="color: #587ea8">NSLog</span>(<span style="color: #e82300">@"JSON:%@"</span>,operation.<span style="color: #587ea8">responseString</span>)<span style="color:#ff0000;">;//打印错误信息</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">        <span style="color: #587ea8">NSLog</span>(<span style="color: #e82300">@"Error: %@"</span>, error);</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="font-size:12px;">    }];</span></p><span style="font-size: 11px;">
</span>
解决办法:

这个错误,你只需要加两句话即可解决:

manager.requestSerializer = [AFHTTPRequestSerializer serializer];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

之后,还需要把收到的responseObject转换一下编码:

NSString *result =  [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];

NSLog(@"JSON:%@",result);

然后,问题解决。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值