繁体   English   中英

错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(可可错误3840.)

[英]Error Domain = NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)

我从web url请求动态json字符串。

-(void)getDataFromServer{

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.domain.com/json/"]];

[request setHTTPMethod:@"GET"];
[request addValue:@"getValues" forHTTPHeaderField:@"METHOD"]; 

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}



-(void)requestReturnedData:(NSData *)data{ //activated when data is returned

 NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:data];

}

我得到了以下错误。

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa  error 3840.)(JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x977a900 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

我用json文本文件测试过

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.domain.com/jsonfile.json"]];

它完美地运作。 我怎样才能克服这个问题。

编辑 - -

我发现,如果json中的行数超过200,则会发生此错误。 否则它运行得很好。 是否存在数据大小问题。

我遇到了同样的问题,并为我的代码找到了解决方案。 当连接返回大数据方法时,“didReceiveData”会在接收到大量数据的情况下多次调用。 我们必须将此方法的数据附加到解析器类的.h文件中声明的NSData引用。 并且应该在NSURLConnection“connectionDidFinishLoading”的委托方法中调用方法“dictionaryWithJSONData”。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[self.dataJSON appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:dataJSON];
}

这里dataJSON在.h文件中声明并在init方法中分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM