我有一个网络服务。当我点击服务器时,几乎需要1分钟才能得到响应。当我在模拟器中运行应用程序时,在50秒内获得响应null。它不会再等几分钟了。我的代码如下所示。
- (void)functionCalling
{
functionName = @"productList";
baseURL = [NSString stringWithFormat:@"http://192.168.0.13/dashly-server/mobile/ Magento-IOS/lib.php?functionName=%@",functionName];
NSLog(@"%@",dateSelect);
NSString *baseString = [NSString stringWithFormat:@"&store_url=%@&api_username=%@&api_key=%@&fromDate=%@&toDate=%@",store_url, api_username, api_key, fDate, tDate];
NSString *completeURL3 = [baseURL stringByAppendingString:baseString];
NSURL *url3 = [NSURL URLWithString:[completeURL3 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"url is %@", url3);
//NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url3];
NSMutableURLRequest *theRequest=[[NSMutableURLRequest alloc]
initWithURL:url3
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:180.0];
theConnection3 = [NSURLConnection connectionWithRequest:theRequest delegate:self];
NSLog(@"connection = %@",theConnection3);
if( theConnection3 )
{
//webData = [NSMutableData dataWithData:[completeURL1 dataUsingEncoding:NSUTF8StringEncoding]];
webData = [NSMutableData dataWithContentsOfURL:url3];
theRequest.HTTPBody = webData;
NSLog(@"web response data %@", webData);
NSString *response3 = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(@"response products = %@", response3);
}
else
{
NSLog(@"theConnection is NULL");
}
}
#pragma mark
#pragma mark Actions - NSURLConnection Methods
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self->webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self->webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%@" , error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
NSString *resStr = [[NSString alloc]initWithData:webData encoding:NSASCIIStringEncoding];
NSLog(@"dash response = %@", resStr);
if ([resStr isEqualToString:@"null"]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"No Products Today..." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
else if ([resStr length]==0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Web services connection failed..." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
else
{
id jsonObject3 = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:&error];
NSArray *array = (NSArray *)jsonObject3;
for(int i=0;i<[array count];i++)
{
dict = [array objectAtIndex:i];
[productIdArray addObject:[dict objectForKey:@"productId"]];
[productNameArray addObject:[dict objectForKey:@"name"]];
[ordersArray addObject:[dict objectForKey:@"nooforders"]];
[priceArray addObject:[dict objectForKey:@"price"]];
}
NSLog(@"product id array %@", productIDArray);
NSLog(@"name array %@", productNameArray);
NSLog(@"orders array %@", ordersArray);
NSLog(@"price array %@", priceArray);
}
}
在50秒时点击服务器后,我收到警报&#34; Web服务连接失败&#34;。即使我发送180秒的时间间隔也不会等待这么长时间。我该如何解决这个问题?我想等到响应来了。我可以在这里使用NSOperationQueue吗?提前谢谢。
答案 0 :(得分:0)
一旦被调用-(void)connectionDidFinishLoading:(NSURLConnection *)connection
而不是-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
,由于服务器端超时,服务器肯定会终止连接。
您可能收到错误的HTTP状态代码。尝试显示它。但是,您应首先考虑提高服务性能,而不是让系统更长时间地工作。