使用AFNetworking實現POST和GET請求時遇到的問題
阿新 • • 發佈:2019-02-14
錯誤原因:
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 = [AFHTTPRequestSerializerserializer];
manager.responseSerializer = [AFHTTPResponseSerializerserializer];
之後,還需要把收到的responseObject轉換一下編碼:
NSString *result = [[NSStringalloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"JSON:%@",result);
然後,問題解決。。。