Ios NSURL连接超时

Ios NSURL连接超时,ios,objective-c,nsurlconnection,nsurl,nsurlrequest,Ios,Objective C,Nsurlconnection,Nsurl,Nsurlrequest,我有一个网络服务。当我点击服务器时,几乎需要1分钟以上才能得到响应。当我在模拟器中运行应用程序时,在50秒内得到空响应。不会再等几分钟了。我的代码如下所示 - (void)functionCalling { functionName = @"productList"; baseURL = [NSString stringWithFormat:@"http://192.168.0.13/dashly-server/mobile/ Magento-IOS/lib.php?fun

我有一个网络服务。当我点击服务器时,几乎需要1分钟以上才能得到响应。当我在模拟器中运行应用程序时,在50秒内得到空响应。不会再等几分钟了。我的代码如下所示

- (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);
    }    
}
-(void)函数调用
{
functionName=@“productList”;
baseURL=[NSString stringWithFormat:@”http://192.168.0.13/dashly-server/mobile/  Magento IOS/lib.php?functionName=%@,functionName];
NSLog(@“%@”,日期选择);
NSString*baseString=[NSString stringWithFormat:@“&store_url=%@&api_用户名=%@&api_键=%@&fromDate=%@&toDate=%@”,store_url,api_用户名,api_键,fDate,tDate];
NSString*completeURL3=[baseURL stringByAppendingString:baseString];
NSURL*url3=[NSURL URLWithString:[completeURL3 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@“url为%@”,url3);
//NSMutableURLRequest*theRequest=[NSMutableUrlRequestWithURL:url3];
NSMutableURLRequest*theRequest=[[NSMutableURLRequest alloc]
initWithURL:url3
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:180.0];
连接3=[NSURLConnection connection with request:theRequest委托:self];
NSLog(@“连接=%@”,即连接3);
如果(连接3)
{
//webData=[NSMutableData dataWithData:[completeURL1 dataUsingEncoding:NSUTF8StringEncoding]];
webData=[NSMutableData dataWithContentsOfURL:url3];
theRequest.HTTPBody=webData;
NSLog(@“web响应数据%@”,webData);
NSString*response3=[[NSString alloc]initWithData:webData编码:NSUTF8StringEncoding];
NSLog(@“响应产品=%@”,响应3);
}
其他的
{
NSLog(@“连接为空”);
}
}
#布拉格标记
#pragma标记操作-NSURLConnection方法
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应{
[self->webData setLength:0];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据{
[self->webData-appendData:data];
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误{
NSLog(@“%@”,错误);
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
n错误*错误;
NSString*resStr=[[NSString alloc]initWithData:webData编码:NSASCIIStringEncoding];
NSLog(@“仪表板响应=%@”,resStr);
如果([resStr isEqualToString:@“null”]){
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:nil消息:@“今天没有产品…”代表:nil CancelButtonTile:@“确定”其他ButtonTiles:nil];
[警报显示];
}
如果([resStr长度]==0),则为else
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:nil消息:@“Web服务连接失败…”委托:nil cancelButtonTitle:@“确定”其他ButtonTitles:nil];
[警报显示];
}
其他的
{
id jsonObject3=[NSJSONSerialization JSONObjectWithData:webData选项:针织错误:&错误];
NSArray*数组=(NSArray*)JSONObject 3;
对于(int i=0;i只要调用的是
-(void)connectionIDFinishLoading:(NSURLConnection*)connection
,而不是
-(void)connection:(NSURLConnection*)connection didfailwitheror:(NSError*)error
,服务器肯定会由于服务器端超时而终止连接


你可能收到了一个坏的HTTP状态代码。试着显示它。但是,你应该首先考虑提高你的服务性能,而不是让你的系统工作更长的时间。< /P>不是一个问题,需要一分钟来显示产品,但是存储基本上是缓慢的,我们不能做任何可能是服务器终止。如果无法在50秒内呈现响应,则请求。但我同意@ffarquet的说法,这是一个很长的等待时间。如果您以这种方式增加超时,连接不应该失败。关闭连接的服务器不是吗?您能打印出您得到的全部错误吗?但就我个人而言,我会在之前重点优化web服务器做一个应用程序。如果你必须等待一分钟以上才能显示内容,我认为你不会得到很多客户端…你想过更好的服务器吗?另一个web平台?缓存?分布式计算?我没有收到任何错误。必须请求服务超时。web响应数据必须为空。