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

Posted

技术标签:

【中文标题】Domain=NSCocoaErrorDomain Code=3840 "操作无法完成【英文标题】:Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed 【发布时间】:2015-01-12 07:36:09 【问题描述】:
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 和 data 中有数据。如何解决这个问题。 我使用了这个链接,但它不起作用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] 

【问题讨论】:

您的 JSON 无效。请确认。你的 json 的结构是什么或提供 url 我已经发布了我的 json 响应。我从 api 获取数据,我无法更改。@MidhunMP 您的 JSON 无效(如果这是您的整个 JSON) 您发布的jsonString不完整。 @WorldOfWarcraft 我更新了我的 jsonString 【参考方案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);


【讨论】:

以上是关于Domain=NSCocoaErrorDomain Code=3840 "操作无法完成的主要内容,如果未能解决你的问题,请参考以下文章

youtube 收到错误渲染模板:Error Domain=NSCocoaErrorDomain Code=258

AFNetworking 文件下载错误 Error Domain=NSCocoaErrorDomain Code=260

checkResourceIsReachableAndReturnError 总是返回 Domain=NSCocoaErrorDomain Code=4 "操作无法完成。(Cocoa 错误

Error Domain = NSCocoaErrorDomain Code=3840 "操作无法完成。(Cocoa 错误 3840。)

JSON 解析错误:Error Domain=NSCocoaErrorDomain Code=3840 “操作无法完成。(Cocoa 错误 3840。)

ios JSON解析错误Error Domain = NSCocoaErrorDomain Code=3840 上传文件并期待响应