【发布时间】:2013-05-24 04:47:52
【问题描述】:
我为此苦苦挣扎了几个小时,我想将一个 json 字符串发送到 asp.net 服务器,但我总是得到空响应。我的代码有什么问题吗?还是服务器端有问题? 非常感谢..
(为了安全起见隐藏了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];
【问题讨论】:
-
在
NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];中传递nil而不是传递NSError*对象并尝试打印错误。 -
请使用jsonlint.com验证您的json json也可能有问题。
-
你的技术看起来很脆弱。容易出错。检查我的正确方法here
标签: iphone ios objective-c xcode json