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

查看:87
本文介绍了使用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];

我的Web服务在post上创建一个新用户并返回userID。
两者都成功进行了Web服务调用(创建了一个新的userID),但是他们没有发布数据,即每次都创建一个空白用户。

如果我遗失请告诉我任何事情。

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

我终于解决了这个问题:
FInally,弄清楚出了什么问题。我使用 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天全站免登陆