简体   繁体   English

如何通过NSURLConnection发送JSON数据

[英]How to send JSON data over NSURLConnection

I have successfully created a method which connects to my web service and POSTS a string and receives a string response. 我已经成功创建了一种方法,该方法连接到我的Web服务并向字符串发送POST并接收字符串响应。 However, I am now trying to POST a JSON dictionary created using NSJSONSerialization. 但是,我现在尝试发布使用NSJSONSerialization创建的JSON字典。 However, Xcode is giving me an error. 但是,Xcode给我一个错误。 I have tried to convert my initial code. 我试图转换我的初始代码。 Relevant lines below... 相关行如下...

NSData* loginDataJSON = [NSJSONSerialization dataWithJSONObject:loginDataDictionary options:NSJSONWritingPrettyPrinted error:&error];
[request setValue:loginDataJSON forHTTPHeaderField:@"loginDataJSON"];
[request setHTTPBody:[loginDataJSON dataUsingEncoding:NSUTF8StringEncoding]];

The second line heres complains that I am using NSData where an NSString is required. 第二行在这里抱怨我在需要NSString的地方使用NSData。 The third line complains that loginDataJSON may not respond to dataUsingEnconding 第三行抱怨loginDataJSON可能不响应dataUsingEnconding

I seem to be forcing an NSData object (because that what NSJSONSerialization gives me) where it cannot be used. 我似乎在强迫NSData对象(因为NSJSONSerialization给了我什么)不能使用它。 Am I best trying to convert the JSON into a string to use with my existing request code, or should/can I change my request code to accept NSData as opposed to an NSString? 我最好是尝试将JSON转换为字符串以与现有请求代码一起使用,还是应该/可以更改请求代码以接受NSData而不是NSString?

Try this code: 试试这个代码:

let body = NSMutableDictionary()
body.setValue("myString" forKey: "stringType")
body.setValue(data?.base64EncodedString(options: .lineLength64Characters) forKey: "dataType")

In this way you can have both data and string in the dictionary. 这样,您可以在字典中同时包含数据和字符串。 Here 'data?.base64EncodedString(options: .lineLength64Characters)' returns you a string with base64 encoding. 在这里, 'data?.base64EncodedString(options: .lineLength64Characters)'将返回具有base64编码的字符串。 So your dictionary contains only string and at server end you have to covert it back to data. 因此,您的字典仅包含字符串,并且在服务器端,您必须将其转换回数据。

Hope it solves your issue. 希望它能解决您的问题。

Have you tried to use frameworks like JSONKit or RESTKit? 您是否尝试过使用JSONKit或RESTKit之类的框架? RESTKit is specially used for webservice communication with JSON and internally uses JSONKit and one another parser. RESTKit专门用于与JSON进行Web服务通信,并且内部使用JSONKit和另一个解析器。 I think this will solve your problem and maybe some future one ;) 我认为这可以解决您的问题,甚至可以解决将来的问题;)

make a mutable request and 提出可变要求并

 [request setHTTPBodyWithString:myMessageJSON];

where method is as follows: 其中方法如下:

- (void)setHTTPBodyWithString:(NSString *)body {
    NSData *bodyData = [body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    [self setValue:[NSString stringWithFormat:@"%d", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
    [self setHTTPBody:bodyData];
}

You need to send the JSON as the body of the request, and maybe set the content type and content length: 您需要发送JSON作为请求的主体,并可能设置内容类型和内容长度:

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:nil];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[request setValue:[NSString stringWithFormat:@"%d", [loginDataJSON length]] forHTTPHeaderField:@"content-length"];
[request setHTTPBody:loginDataJSON];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM