【问题标题】:iOS 5 Json SerializationiOS 5 Json 序列化
【发布时间】:2013-06-11 13:16:47
【问题描述】:

我已经使用 JSON 序列化来获取 json 响应,在这里我一切都很好,但是当我需要将一些值作为键值对与 URL 一起发布时。我已经这样做了,但没有得到结果。

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

我不知道这里出了什么问题...请各位大侠帮帮我..

【问题讨论】:

  • 你没有指出任何错误检查是否发现了什么。

标签: ios json


【解决方案1】:

您的代码中有多个错误,请使用我的代码作为参考并将其与您的代码进行比较,您将得到由您完成的错误。 从 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:使用我提供的代码。
  • 我更改了网址,但不起作用
  • 这是 o/p: 2013-06-11 19:19:35.545 JSON5[24317:15d03] str ::
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 2012-01-26
  • 2013-02-04
  • 1970-01-01
  • 2016-01-30
相关资源
最近更新 更多