我的 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/