1
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=0x7fac23c3a250 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

i am using this code

NSString *jsonString = [NSString stringWithContentsOfURL:url1 encoding:NSUTF8StringEncoding error:nil];
   //  NSError *err;
    NSLog(@"the jsonString==%@",jsonString);

    NSError *jsonError;
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data
                                                                 options:NSJSONReadingMutableContainers
                                                                   error:&jsonError];
     NSLog(@"jjsonError=======================%@",jsonError);

getting jsonResponse null. and i have data in jsonString and data . how to solve this issue . i used this link but its not working Cocoa error 3840 using JSON (iOS)

jsonString i am getting like this

    jsonString ==j==jsonFlickrApi({"sizes":{"canblog":0,"canprint":0,"candownload":0,"size":[{"label":"Square","width":75,"height":75,"source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_s.jpg","url":"https:\/\/www.flickr.com\/photos\/124934434029@N02\/14103359278\/sizes\/sq\/","media":"photo"},{"label":"Large Square","width":"150","height":"150","source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_q.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/q\/","media":"photo"},{"label":"Thumbnail","width":"100","height":"66","source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_t.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/t\/","media":"photo"},{"label":"Small","width":"240","height":"159","source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_m.jpg","url":"https:\/\/www.flickr.com\/photos\/12444934029@N02\/14103359278\/sizes\/s\/","media":"photo"},{"label":"Small 320","width":"320","height":213,"source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_n.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/n\/","media":"photo"},{"label":"Medium","width":"500","height":"332","source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/m\/","media":"photo"},{"label":"Medium 640","width":"640","height":"425","source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_z.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/z\/","media":"photo"},{"label":"Medium 800","width":"800","height":531,"source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_c.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/c\/","media":"photo"},{"label":"Large","width":"1024","height":"680","source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_239cffa6c4_b.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/l\/","media":"photo"},{"label":"Large 1600","width":"1600","height":1063,"source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_cce6d96d28_h.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/h\/","media":"photo"},{"label":"Large 2048","width":"2048","height":1360,"source":"https:\/\/farm3.staticflickr.com\/2929\/14103359278_c6218398b6_k.jpg","url":"https:\/\/www.flickr.com\/photos\/124934029@N02\/14103359278\/sizes\/k\/","media":"photo"}]},"stat":"ok"})
2015-01-12 13:12:59.320 jsonsamp[1225:690335] 

enter image description here

Community
  • 1
  • 1
  • 3
    Your JSON is not a valid one. Please verify that. What is the structure of your json or provide the url – Midhun MP Jan 12 '15 at 07:37
  • i have posted my json respond . i am getting data from api there i cant able to change.@MidhunMP –  Jan 12 '15 at 07:46
  • Your JSON is not valid (If that is your entire JSON) – Midhun MP Jan 12 '15 at 07:50
  • The jsonString you had post is not complete. – Zigii Wong Jan 12 '15 at 07:51
  • The first character of your JSON should be the {, and the last should be the }. It looks like you have extra stuff before and after. It also doesn't exactly match what you have in your NSLog statement, so it's hard to tell what is transcription/editing error, and what is a genuine error. – Rob Jan 12 '15 at 08:28
  • how to solve this in that case @Rob –  Jan 12 '15 at 08:56
  • i have used postman plugin getting the same json format.@Rob,@Midhun MP –  Jan 12 '15 at 11:18
  • If your server is sending "jsonFlickrApi(...)" as part of the response, then that is not correct. Your problem would appear to be the server code (which you haven't shared with us), not the client code. – Rob Jan 12 '15 at 11:37
  • i can not able to see sever code and api that is my issue –  Jan 12 '15 at 11:49
  • See also http://stackoverflow.com/questions/26233611/error-domain-nscocoaerrordomain-code-3840-the-operation-couldn-t-be-completed-u – Richard Chambers Apr 04 '16 at 03:28

1 Answers1

1

you try this code, maybe it's working fine for you. First you have check the API using Advanced REST client.

NSString *post =[[NSString alloc] initWithFormat:@"shop_name=%@&title=%@",shopname,chatType];

NSLog(@"PostData: %@",post);

 NSURL *url=[NSURL URLWithString:@"http://localhost/load_tailors"];


NSLog(@"%@",url);

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"Response code: %ld", (long)[response statusCode]);


if ([response statusCode] >= 200 && [response statusCode] < 300)
{
    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"Response ==> %@", responseData);

    NSError *error = nil;
   jsonData = [NSJSONSerialization
                              JSONObjectWithData:urlData
                              options:NSJSONReadingMutableContainers
                              error:&error];
    NSLog(@"%@",jsonData);

}
Rahul Saini
  • 921
  • 2
  • 10
  • 23