开发者

NSMutableURLRequest sending twice

开发者 https://www.devze.com 2023-01-23 22:37 出处:网络
My NSMutableURLRequest is sending the strings twice to PHP. What am I doing wrong? - (void)viewDidLoad {

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, sel开发者_如何学JAVAf.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];


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.


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


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

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

Stopping a Connection

0

精彩评论

暂无评论...
验证码 换一张
取 消