2010-12-23 174 views
0

我試圖從爲我一直在努力的項目創建的Web服務訪問數據。 我創建一個字符串創建NSURLConnection sendSynchronousRequest花費太多時間來響應

NSMutableString *sRequest = [[NSMutableString alloc] init]; 
[sRequest appendString:@"<soapenv:Envelope xmlns:soapenv=\"http:blah blah blah /messages\">"]; 
[sRequest appendString:@"<soapenv:Header/>"]; 
[sRequest appendString:@"<soapenv:Body>"]; 
[sRequest appendString:@"<mes:processActionRequest>"]; 
[sRequest appendString:@"<ProcessAction>"]; 
[sRequest appendString:@"</mes:processActionRequest>"]; 
[sRequest appendString:@"</soapenv:Body>"]; 
[sRequest appendString:@"</soapenv:Envelope>"]; 

然後我創建一個URL,並請求

NSURL *ServiceURL = [NSURL URLWithString:[[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"webservices" ofType:@"plist"]] valueForKey:@"EndPointURL"]]; 



NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:ServiceURL]; 

[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; 



[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]]; 

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate]; 

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL]; 

NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - start; 
NSLog(@"Response Time%.4f",duration); 

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 


return responseData; 

的問題是一個請求,即我在登錄打印的響應時間總是爲高於20秒不知何故。但是,當我使用eviware/SoapUI項目訪問同一鏈接時,它會在一秒鐘內執行,而且當黑莓項目中使用相同的鏈接時,它也可以順利運行。我沒有得到,我錯在哪裏。爲什麼iPhone的響應速度非常慢。請幫忙。 如果我聽起來不太清楚,請告訴我。我會盡力詳細說明。

回答

1

除非您已經從後臺線程運行代碼,否則最好在NSURLConnection上使用異步調用。

您是否看到與異步API相同的問題?

+0

我曾嘗試使用異步API。它顯示了相同的延遲響應。 – jason 2010-12-24 07:46:10

3

在導致問題的url結尾處有一個新行字符。感謝支持安德魯。

相關問題