objective-c - NSMutableURLRequest 发送两次

标签 objective-c ios cocoa-touch http-post nsurlrequest

我的 NSMutableURLRequest正在向 PHP 发送两次字符串。我究竟做错了什么?

- (void)viewDidLoad {

// Printando os Dados

/////

NSString *endereco = [[NSString alloc] initWithFormat:@"%@ - CEP: %@", self.sharedData.dataEndereco, self.sharedData.dataCEP];


NSMutableURLRequest *request = 
[[NSMutableURLRequest alloc] initWithURL:
 [NSURL URLWithString:@"http://localhost/dev/mcomm/pedido.php"]];
NSLog(@"ENVI");

[request setHTTPMethod:@"POST"];
NSString *postString = [[NSString alloc] initWithFormat:@"nome=%@&email=%@&endereco=%@&produto=%@&marca=%@&preco=%@&codigo=%@&variacao=%@&parcelas=%@&valorParcelas=%@&cartao=%@&numCartao=%@&codCartao=%@&vencCartao=%@&nomeCartao=%@", self.sharedData.dataNome, self.sharedData.dataEmail, endereco, self.sharedData.dataProd, self.sharedData.dataMarca, self.sharedData.dataPreco, self.sharedData.dataCodigo, self.sharedData.dataVariacao, self.sharedData.numParcelas, self.sharedData.valorParcelas, self.sharedData.cartao, self.sharedData.numeroCartao, self.sharedData.codigoCartao, self.sharedData.dataVencimento, self.sharedData.nomeImpressoCartao];

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

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

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

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[loaderAtividade startAnimating];


//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

最佳答案

第一个请求由以下发起:

[[NSURLConnection alloc] initWithRequest:request delegate:self];
第二个是由函数发送的:
 [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
请注意,来自 apple documentation关于 sendSynchronous:

Important: Because this call can potentially take several minutes to fail (particularly when using a cellular network in iOS), you should never call this function from the main thread of a GUI application.


您应该实现 NSURLConnection 委托(delegate)协议(protocol)并避免发送同步请求。

关于objective-c - NSMutableURLRequest 发送两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4109108/

相关文章:

iphone - 使用 UIProgressView 进行图像编辑

ios - Objective-C 桥接 header 不适用于 Swift 文件

ios - 如何在点击PresentationLink (SwiftUI) 时禁用闪烁?

objective-c - UIImageView 中的 JPG 图像内存占用

objective-c - iOS 清理错误的 XML

ios - 如何在 iOS 上绘制嵌套的径向渐变?

c# - "ConvertPdbToMdb"任务意外失败

ios - QuickBlox 推送通知权限警报时间自定义

ios - 二元运算符 '|' 不能应用于两个 UIViewAutoresizing 操作数

iphone - 不会触发任何与方向相关的通知。为什么?