使用 Json 在 Objective C 中发布数据 [英] Post data in Objective C using Json

查看:23
本文介绍了使用 Json 在 Objective C 中发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据发布到 PHP Web 服务.
我很熟悉使用查询 $.post 在 html 中执行此操作,但我很难在目标 C 中尝试此操作.我尝试了几个博客 &在 stackoverflow 上发现的问题.

I am trying to post data to a PHP web service.
I am familiar doing this in html using query $.post but I am very much stumped trying this in objective C. I tried several blogs & questions found on stackoverflow.

我终于想出了以下代码:

I finally came up with the following code:

NSString *jsonRequest = [NSString stringWithFormat:@"{"Email":"%@","FirstName":"%@"}",user,fname];
NSLog(@"Request: %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"http:myurl..."];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

我也试过:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

I also tried:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

我的网络服务在发布时创建一个新用户并返回用户 ID.两者都成功地进行了 Web 服务调用(创建了一个新的用户 ID),但是它们没有发布数据,即每次都创建一个空白用户.
如果我遗漏了什么,请告诉我.

My web service creates a new user on post and returns the userID. Both successfully make the web service call(a new userID is created), however they do not post the data, i.e. a blank user is created every time.
Please tell me if I am missing anything.

谢谢.

我的其他尝试:

NSMutableURLRequest *request = 
[[NSMutableURLRequest alloc] initWithURL:
 [NSURL URLWithString:@"myUrl.. "]];

[request setHTTPMethod:@"POST"];

NSString *postString = @"Email=me@test.com&FirstName=Test";

[request setValue:[NSString 
                   stringWithFormat:@"%d", [postString length]] 
forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[postString 
                      dataUsingEncoding:NSUTF8StringEncoding]];

[[NSURLConnection alloc] 
 initWithRequest:request delegate:self];

我终于解决了这个问题:终于知道是哪里出了问题.我使用 http://mypath?params=123 作为我的网址.我没有演出完整的网址(在其他语言中从不需要它).但在这里我需要给 htt://mypath/index.php?params=123

I finally solved the issue: FInally, figured out what was wrong. I was using http://mypath?params=123 as my url. I did not gig the complete url(never needed it in other languages). But here I needed to give htt://mypath/index.php?params=123

推荐答案

我认为你最好像这样使用 NSJSONSerialization 类:

I think you would be better off using the NSJSONSerialization class like this:

NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
                     email, @"Email",
                     fname, @"FirstName",
                     nil];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
[request setHTTPBody:postData];

使用字典然后将其转换为 JSON 比像字符串一样创建它更容易.

Using a dictionary and then converting it to JSON it's easier than creating it like a string.

祝你好运!

[SWIFT 3.0](更新)

let tmp = ["email": email,
           "FirstName": fname]
let postData = try? JSONSerialization.data(withJSONObject: tmp, options: .prettyPrinted)
request.httpBody = postData

这篇关于使用 Json 在 Objective C 中发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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