NSMutableURLRequest 发送两次

发布于 2024-10-01 00:56:49 字数 1885 浏览 1 评论 0原文

我的 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];

My NSMutableURLRequest is sending the strings twice to PHP. What am I doing wrong?

- (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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风不识月 2024-10-08 00:56:50

除了丹尼尔的回答之外,您还可以通过实现 initWithRequest:delegate:startImmediately: with NO 来防止第一个请求触发

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

然后稍后,当您想启动异步请求时,只需在连接对象上调用 -start

[urlConnection start];

In addition to Daniel's answer, you can prevent the first request from firing by implementing initWithRequest:delegate:startImmediately: with NO

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

Then later, when you'd like to start your asynchronous request, you just call -start on the connection object

[urlConnection start];
愿与i 2024-10-08 00:56:50
NSURLConnection *conn = .....
if(conn){
    ......

    [conn cancel];//! important!
    [conn release];
}

停止连接

NSURLConnection *conn = .....
if(conn){
    ......

    [conn cancel];//! important!
    [conn release];
}

Stopping a Connection

七秒鱼° 2024-10-08 00:56:49

第一个请求由以下人员发起:

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

第二个请求由该函数发送:

 [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

注意事项,来自 关于 sendSynchronous 的苹果文档

重要提示:由于此调用可能需要几分钟才能失败(特别是在 iOS 中使用蜂窝网络时),因此您永远不应该从 GUI 应用程序的主线程调用此函数。

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

The first request is initiated by:

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

And the second is sent by the function:

 [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

A word of caution, from apple documentation about the 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.

You should implement the NSURLConnection delegate protocol and avoid sending a synchronous request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文