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

StackOverflow https://stackoverflow.com/questions/23147950

Question

I am requesting dynamic json string from web url.

-(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

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. Otherwise it runs perfectly. is there a issue with data size.

Was it helpful?

Solution

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. We have to append the data of this method to NSData reference declared in .h file of the parser class. And should call the method "dictionaryWithJSONData" in the delegate method of NSURLConnection "connectionDidFinishLoading".

- (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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top