【问题标题】:Error while calling webservice. NSCocoaErrorDomain Code=3840调用网络服务时出错。 NSCocoaErrorDomain 代码=3840
【发布时间】:2015-06-26 18:08:01
【问题描述】:

我正在开发一个应用程序。调用我的一项网络服务时出现错误,

 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7f9200696fc0 {NSDebugDescription=Invalid value around character 0.}

这是我的代码:-

NSError *error;
NSURLResponse *response;

NSURL *post=[NSURL URLWithString:myUrl];

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:post cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:50];

[request setHTTPMethod:@"POST"];
[request addValue:@"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:@"Content-Type"];

NSString *body=[NSString stringWithFormat:@"userId=%@",userId];

[request setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (data==nil) {
    return;
}


NSLog(@"data from JSON =%@",data);

当我打印我的 NSData 时,我在解析中遇到了一些错误,

<b>Parse error</b>:  syntax error, unexpected end of file, expecting function (T_FUNCTION) in <b>/var/www

现在,我能做些什么来解决这个问题?有人可以建议吗?

【问题讨论】:

  • NSLog response, error 并将它们添加到问题中。
  • 我一直想知道为什么开发人员会这样做:allowLossyConversion:YES。数据丢失真的是个好主意吗? NSASCIIStringEncoding 不再是一个好主意,通常最好使用NSUTF8StringEncoding,这将允许其他语言甚至 表情符号。
  • 感谢纠正我。我对 iOS 很陌生,对大多数方法都不熟悉,但现在我将了解这些方法。
  • 我已经在第一行给出了 NSLog 错误 Error Domain=NSCocoaErrorDomain Code=3840 “操作无法完成。(Cocoa 错误 3840。)”(字符 0 周围的值无效。) UserInfo=0x7f9200696fc0 {NSDebugDescription=字符 0 附近的值无效。}
  • 您没有提供响应,响应数据中有http状态码。

标签: ios nserror


【解决方案1】:

检查http状态码很重要,服务器可以返回一个服务器特定的响应码。

Status Code Definitions。请注意,许多服务器定义了特定于应用程序的 5xx 错误代码。

【讨论】:

  • 您有任何公开的联系信息吗?
猜你喜欢
  • 2017-06-16
  • 2018-04-08
  • 2015-03-11
  • 2020-03-14
  • 2016-02-26
  • 1970-01-01
  • 2016-07-10
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多