iOS 5 Json序列化 [英] iOS 5 Json Serialization

查看:76
本文介绍了iOS 5 Json序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used JSON Serialization to get json response, here i'mn getting all fine, but when i need to post some values as key value pair with the URL. I have done like this, but didn't get the result.

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

我不知道这里出了什么问题...请伙计帮助我..

I don't know what goes wrong here... Please dudes help me..

推荐答案

您的代码中存在多个错误,使用我的代码作为参考并将其与您的代码进行比较,您将会得到你做的错误。
以下代码从Objective-C的角度来看是正确的。您的网址或服务端存在一些错误。

There are multiple Errors in your Code, Use my Code as a Reference and compare it to yours and you'll get the Errors done by you. The Below code is working correctly from the Point of View of Objective-C. There are some Errors regarding your URL or Service Side.

工作代码:

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

这篇关于iOS 5 Json序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆