[英]Error Domain = NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)
I am requesting dynamic json string from web url. 我从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];
}
I got below error. 我得到了以下错误。
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.}
I have tested with json text file 我用json文本文件测试过
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.domain.com/jsonfile.json"]];
It works perfectly. 它完美地运作。 How can i overcome this issue.
我怎样才能克服这个问题。
Edit--- 编辑 - -
I have found that , if number of rows in json exeeding 200 this error happens. 我发现,如果json中的行数超过200,则会发生此错误。 Otherwise it runs perfectly.
否则它运行得很好。 is there a issue with data size.
是否存在数据大小问题。
I got the same problem and found the solution for my code. 我遇到了同样的问题,并为我的代码找到了解决方案。 When connection returns large data method "didReceiveData" calls many time with the chunk of data recieved.
当连接返回大数据方法时,“didReceiveData”会在接收到大量数据的情况下多次调用。 We have to append the data of this method to NSData reference declared in .h file of the parser class.
我们必须将此方法的数据附加到解析器类的.h文件中声明的NSData引用。 And should call the method "dictionaryWithJSONData" in the delegate method of NSURLConnection "connectionDidFinishLoading".
并且应该在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];
}
Here dataJSON is declared at .h file and allocated in the init method. 这里dataJSON在.h文件中声明并在init方法中分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.