问题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);
}];
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);
然后,问题解决。。。