简体   繁体   English

iOS将字符串发送到JSON Server

[英]iOS send string to JSON Server

im struggling this for several hours, i want to send a json string to an asp.net server, but i always get null response. 我挣扎了几个小时,我想发送一个json字符串到asp.net服务器,但我总是得到null响应。 Is there anything wrong with my code?or problem in the server side? 我的代码有什么问题吗?还是服务器端的问题? thanks a lot.. 非常感谢..

(url is hidden for security only), and this is the update code.. (仅为安全隐藏url),这是更新代码..

NSError *error;


NSDictionary* jsonDict = @{@"FundCode": @(1), @"TotalAmount":@(1000000), @"PaymentType":@(0), @"Bank":@"AAA BANK",@"BankAccountNo": @"123456789",@"Amount":@"1000000",
                           @"DepositFile":@"asset.PNG"};

NSData* postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&error];


NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.url.com"]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url1];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

You could try to use Cocoa's built-in JSON serialization to obtain your JSON data instead of providing an escaped string yourself. 您可以尝试使用Cocoa的内置JSON序列化来获取JSON数据,而不是自己提供转义字符串。 This rules out improper escaping etc. 这排除了不正当的逃逸等。

NSError* error = nil;
NSDictionary* jsonDict = @{@"FundCode": @(1), @"TotalAmount":@(1000000), @"PaymentType":@(0), @"Bank":@"AAA BANK"};
NSData* postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&error];

Please note, that I didn't specify all keys - You'd need to complete the dictionary before sending the request. 请注意,我没有指定所有密钥 - 您需要在发送请求之前完成字典。

friend you are passing wrong method(as u sending it just as a NSString) for JSON use this method 朋友你传递错误的方法(因为你发送它只是作为一个NSString)为JSON使用这个方法

NSError* errorJson = nil;
NSDictionary* jsonDict = @{@"FundCode": @(1), @"TotalAmount":@(1000000), @"PaymentType":@(0), @"Bank":@"AAA BANK",@"BankAccountNo": @"123456789",@"Amount":@@"1000000",
@"DepositFile":@"asset.PNG"};
NSData* postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&errorJson];

Hope this helps.. 希望这可以帮助..

You can also use NSKeyArchiever to encode NSDictionary json to postData as given below 您还可以使用NSKeyArchiever将NSDictionary json编码为postData,如下所示

NSMutableDictionary *postDix=[[NSMutableDictionary alloc] init];
[postDix setValue:@"1" forKey:@"FundCode"];
[postDix setValue:@"1000000" forKey:@"TotalAmount"];
[postDix setValue:@"0" forKey:@"PaymentType"];    //etc

NSMutableData *postData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:postData];
[archiver encodeObject:postDix forKey:@"json"];
[archiver finishEncoding];

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

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