Domain = NSCocoaErrorDomain Code = 3840“无法完成操作

时间:2015-01-12 07:36:10

标签: ios objective-c iphone nsjsonserialization

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

我正在使用此代码

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);

让jsonResponse为空。我有jsonString和数据中的数据。如何解决这个问题。 我使用了这个链接,但它不起作用Cocoa error 3840 using JSON (iOS)

jsonString 我变得像这样

    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

1 个答案:

答案 0 :(得分:1)

你试试这个代码,也许它对你很好。首先,您使用Advanced REST client检查API。

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);

}