iOS5 Json序列化
我已经使用JSON序列化来获得JSON响应,在这里我会得到所有的好结果,但是当我需要发布一些值作为URL的键值对时。我已经这样做了,但是没有得到结果iOS5 Json序列化,ios,json,Ios,Json,我已经使用JSON序列化来获得JSON响应,在这里我会得到所有的好结果,但是当我需要发布一些值作为URL的键值对时。我已经这样做了,但是没有得到结果 NSArray *objects = [NSArray arrayWithObjects:@"uname", @"pwd", @"req",nil]; NSArray *keys = [NSArray arrayWithObjects:@"ann", @"ann", @"login", nil]; NSDictionary *dict = [NSD
NSArray *objects = [NSArray arrayWithObjects:@"uname", @"pwd", @"req",nil];
NSArray *keys = [NSArray arrayWithObjects:@"ann", @"ann", @"login", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:keys forKeys:objects];
if ([NSJSONSerialization isValidJSONObject:dict]) {
NSError *error;
result = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error == nil && result != nil) {
// NSLog(@"Success");
}
}
NSURL * url =[NSURL URLWithString:@"URL_address_VALUE/index.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[result length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:result];
NSURLResponse *res = nil;
NSError *error = nil;
NSData *ans = [NSURLConnection sendSynchronousRequest:request returningResponse:&res error:&error];
if (error == nil) {
NSString *strData = [[NSString alloc]initWithData:ans encoding:NSUTF8StringEncoding];
NSLog(@"%@",strData);
}
我不知道这里出了什么问题。。。请各位帮帮我。您的代码中有多个错误,请使用我的代码作为参考,并将其与您的代码进行比较,您将完成错误处理。 从Objective-C的角度来看,下面的代码工作正常。关于您的URL或服务端,存在一些错误 工作代码:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"ann",@"uname",@"ann",@"pwd",@"login",@"req", nil];
NSLog(@"dict :: %@",dict);
NSError *error2;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error2];
NSString *post = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"postLength :: %@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://exemplarr-itsolutions.com/dbook/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error3;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error3];
NSString *str = [[NSString alloc] initWithData:POSTReply encoding:NSUTF8StringEncoding];
NSLog(@"str :: %@",str);
NSLog没有显示任何内容@Vin@HarishSaran:检查我的更新。@HarishSaran:使用我提供的代码。我更改了URL,但不起作用。这是o/p:2013-06-11 19:19:35.545 JSON5[24317:15d03]str::您没有指出任何错误检查是否捕获了任何内容。