解析 Curl Api 时出错
Posted
技术标签:
【中文标题】解析 Curl Api 时出错【英文标题】:Error When Parsing Curl Api 【发布时间】:2014-12-09 05:36:06 【问题描述】:我已经实现了这段代码。当我第一次运行它时,它会给出响应。但是当我第二次运行它时,它会给出错误......
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0x178265540 NSDebugDescription=No value.
但是每当我卸载并重建它时,它都会给出响应。
NSMutableArray *data=[[NSMutableArray alloc]initWithObjects:@"type",@"st_eml",@"st_pwd",nil];
NSMutableArray *value=[[NSMutableArray alloc]initWithObjects:@"normal", @"Huhn",@"1234", nil];
//NSArray *keys = [[NSArray alloc ]initWithObjects:@"type", @"st_eml", @"st_pwd", nil];
//NSArray *objects = [[NSArray alloc]initWithObjects:@"normal", @"Huhn",@"123", nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:value forKeys:data];
// Serialize the data
NSError *error = NULL;
NSData *theJSONData = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary error:&error];
NSLog(@"Serialization Error: %@", error);
// Change the data back to a string
NSString* theStringObject = [[NSString alloc] initWithData:theJSONData encoding:NSUTF8StringEncoding];
// Determine the length of the data
NSData *requestData = [theStringObject dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%lu", (unsigned long)[requestData length]];
// Create request to send to web service
NSString *strurl = @"http://testing.singletreffen.de/appapi/v1/login";
NSString* webStringURL = [strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:webStringURL]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval:30.0];
// Deserialize the response
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error:&error];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
【问题讨论】:
【参考方案1】:我已经尝试过您的代码,对解析进行了一些更改,它可以正常工作并给出响应
NSMutableArray *data=[[NSMutableArray alloc]initWithObjects:@"type",@"st_eml",@"st_pwd",nil];
NSMutableArray *value=[[NSMutableArray alloc]initWithObjects:@"normal", @"Huhn",@"1234", nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:value forKeys:data];
// Serialize the data
NSError *error = nil;
NSData *theJSONData = [NSJSONSerialization dataWithJSONObject:theRequestDictionary options:0 error:&error];
NSLog(@"Serialization Error: %@", error);
// Change the data back to a string
NSString* theStringObject = [[NSString alloc] initWithData:theJSONData encoding:NSUTF8StringEncoding];
// Determine the length of the data
NSData *requestData = [theStringObject dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%lu", (unsigned long)[requestData length]];
// Create request to send to web service
NSString *strurl = @"http://testing.singletreffen.de/appapi/v1/login";
NSString* webStringURL = [strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:webStringURL]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval:30.0];
// Deserialize the response
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error:&error];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"LOG : %@", returnString);
【讨论】:
它给了一些时间,但有些时候仍然是同样的问题。以上是关于解析 Curl Api 时出错的主要内容,如果未能解决你的问题,请参考以下文章