【问题标题】:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed in iOS using AFNetworkingError Domain=NSCocoaErrorDomain Code=3840 "使用 AFNetworking 在 iOS 中无法完成该操作
【发布时间】:2015-07-13 03:41:10
【问题描述】:

我正在使用 AFNetworking 在 iOS 中使用 post 方法从服务器获取数据,但我得到状态代码 200 OK 但在 AFNetworking 中它来自失败方法及其显示下面的错误我也在谷歌上搜索过,我也根据结果进行了尝试,但它显示相同的错误。

等待您的回复。

状态码:-

{ status code: 200, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html"

错误:- Error Domain=NSCocoaErrorDomain Code=3840 “操作无法完成。(Cocoa 错误 3840。)”(字符 0 周围的值无效。) UserInfo=0x7f838a5df950 {NSDebugDescription=字符 0 周围的值无效。}

这是我的代码:-

NSError *error;
NSDictionary *result;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictPost
                options:kNilOptions    error:&error];
if (! jsonData) {
} else {

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

}

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager setResponseSerializer:responseSerializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];*


[manager POST:@“http:www.server.url.com” parameters:result success:^(AFHTTPRequestOperation *operation, id responseObject) {

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


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@“Error: %@", error.description);

}];

【问题讨论】:

  • 能否重新格式化代码?
  • 你解决过这个问题吗?我有同样的问题,我在这个链接上尝试过的所有解决方案都没有奏效--stackoverflow.com/questions/19114623/…
  • @Allen 我已经解决了上述问题。

标签: ios objective-c afnetworking


【解决方案1】:

我认为你的问题与这个answer有关。

响应本身可能有错误,无法解析为有效的 JSON。

我会尝试使用第 3 方工具(例如 Postman for Chrome)来调试服务器响应。

更新: 您的内容类型应为application/json

【讨论】:

  • 感谢您的回复,但我已经根据您的参考 URL 进行了更改,并且当我使用表单数据中的工具发送请求时,我还检查了 Postman 和 Restclient,然后我得到响应成功,但是当我使用发送请求时来自iOS编程的表单数据然后它给了我错误。我得到成功代码200 OK但我没有得到响应我们在POSTMAN和RESTclient中得到任何想法?
猜你喜欢
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 2012-02-11
  • 2015-05-19
  • 1970-01-01
  • 2011-08-27
相关资源
最近更新 更多