【发布时间】:2016-02-11 05:43:35
【问题描述】:
嗨,我正在尝试将 json 请求传递给服务器。我为此使用了 api,我必须在我的 api 中传递 json 参数,但我不知道如何在 api 中传递 json 参数,我知道我正在使用 AFNetworking在我使用过的代码下面传递 json 参数。请帮我解决这个问题提前谢谢..
参数格式如下
{
"email" : "abc@gmail.com",
"phone" : "1234567890",
"password" : "aaa",
"medium" : "Email",
"name" : "abc",
"uos" : "i"
}
代码:
services *srv=[[services alloc]init];
NSString *str=@"http://emailsending.in/setting_saver_api/"; NSString *method=@"registration.php";
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:self.txtName.text forKey:@"name"];
[dict setObject:self.txtEmail.text forKey:@"email"];
[dict setObject:self.txtphone.text forKey:@"phone"];
[dict setObject:self.txtPassword.text forKey:@"password"];
[dict setObject:@"Email" forKey:@"medium"];
[dict setObject:@"i" forKey:@"uos"];
NSString *jsonString;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json string :%@",jsonString);
}
[srv postToURL:str withMethod:method andParams:jsonString completion:^(BOOL success, NSDictionary *responseObj)
{
NSLog(@"res :%@",responseObj);
NSLog(@"%d",success);
NSLog(@"Successfully..................");
}];
【问题讨论】:
-
我不知道,但数据没有传递到服务器,但我得到了 json 格式的转换字符串,我将其作为参数传递,我得到的响应是状态 = 0 和 msg = 需要数据
-
github.com/AFNetworking/AFNetworking 在 github 上参考这个。你会发现 - JSON 参数编码。
标签: ios objective-c json web-services afnetworking