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

我正在从url请求dynamicjsonstring。

-(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文本文件进行了testing

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

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

编辑 – –

我发现,如果json中的行数超过200这个错误发生。 否则它运行完美。 数据大小有问题吗?

我得到了同样的问题,并find了我的代码的解决scheme。 当连接返回大数据方法“didReceiveData”与大块数据接收时调用很多时间。 我们必须将这个方法的数据附加到parsing器类的.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方法中分配。