2014-11-14 80 views
1

我有一个URL,我试图从中获取XML。NSURLConnection返回无数据

现在在我的iOS应用程序,我有这个,获取数据。

- (void)viewDidLoad { 
[super viewDidLoad]; 
    [self loadDataUsingNSURLConnection]; 
} 

- (void) loadDataUsingNSURLConnection { 
    NSString *url = @"http://64.182.231.116/~spencerf/university_of_albany/u_albany_alumni_menu_test.xml"; 
       [self getMenuItems:url]; 
} 

然后终于此,

- (void)getMenuItems:(NSString*)url{ 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    NSLog(@"response == %@", response); 

    NSLog(@"data: %@", data); 
    /* 
    self.mealdata=[[MealData alloc]init:data]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tableView reloadData]; 
     [self.loadingView removeFromSuperview]; 
    }); 
    */ 

}]; 
} 

现在有时当我运行我的应用程序它的伟大工程,并返回数据,但后来有时候,我会说的时候约25%,与我改变运行之间的任何内容。它没有返回数据,并NSLog回报

2015-03-10 18:28:05.472 APP[6289:97905] response == <NSHTTPURLResponse: 

0x7fee7628e000> { URL: http://64.182.231.116/~spencerf/university_of_albany/u_albany_alumni_menu_test.xml } { status code: 200, headers { 
    "Accept-Ranges" = bytes; 
    Connection = "Keep-Alive"; 
    "Content-Length" = 0; 
    "Content-Type" = "application/xml"; 
    Date = "Tue, 10 Mar 2015 22:28:04 GMT"; 
    Etag = "W/\"218ceff-0-510f6aa0317dd\""; 
    "Keep-Alive" = "timeout=5, max=90"; 
    "Last-Modified" = "Tue, 10 Mar 2015 22:28:03 GMT"; 
    Server = Apache; 
} } 


2015-03-10 18:28:05.472 App[6289:97905] data: <> 

不知道为什么发生这种情况,我不能告诉它工作时,当它没有什么改变之间的区别?所以我不知道如何解决这个问题?

我想要的是每次都得到数据吗?

感谢您的帮助。

+1

我在你的网址上得到了一个404 – 2015-03-17 20:17:29

回答

1

你的代码没问题。这是给你麻烦的服务器。

我重新加载了您提到过的20次页面。它成功加载了13次。 7次它返回没有错误,但也是一个空的响应体。

您可以做的只是检查您的应用程序中的这种情况,然后再次运行请求以再次尝试。

或者与服务器所有者交谈,以确定他们是否可以提高此服务的可靠性。

+0

噢,我看到,发生了什么事情是该文件是从脚本生成的,我已经运行了cron作业,每minut运行一次e,所以当它运行时,页面正在更新约5秒钟。您介意告诉我如何检查它是否正在更新并再次尝试请求? – iqueqiorio 2015-03-11 03:05:19

+0

Stefan你有什么建议吗? – iqueqiorio 2015-03-12 19:53:17

+0

我的建议是,如果您收到零长度响应,请重新运行该请求。您可以再次执行相同的代码。例如,保持一个柜台来跟踪你重试了多少次。并且在请求之间使用一个'NSTimer'稍微延迟一下。 - 你需要帮助吗? – 2015-03-12 20:00:18

1

运行你上设备的代码,当有接收的数据,错误消息是错误域= NSURLErrorDomain代码= -1005“网络连接已丢失”。

至于后面的错误,你可以看到AFNetworking/issues/2314,下面是从帖子中提取的评论。

当iOS用户端接收到具有保持活动头的HTTP响应,它 保持该连接再次使用后(因为它应该),但它保持它 为比备存─的超时参数更活动标题,然后 当第二个请求到来时,它会尝试重新使用服务器已删除 的连接。

你也可以参考的解决方案here,如果你不能改变服务器,我建议你重试请求时,错误代码为-1005或接收的数据长度为0

+0

我没有收到错误代码-1005?并不确定你的意思是什么? – iqueqiorio 2015-03-11 04:13:48

+0

我记录了错误信息,我看到了。但它发生在我身上。 。我使用iOS 8.1.3在iPhone 6上测试了代码,我不知道为什么你没有看到它,它可能不会发生在你身上。 – gabbler 2015-03-11 06:31:02

+0

我运行的是同样的东西,你能告诉我你记录了什么吗?像什么是代码行? – iqueqiorio 2015-03-11 17:11:13

1

也许长投票将帮助你。当我尝试它时,Web服务器会间歇性地返回数据。

长轮询将允许您继续发送请求,直到获得您指定的数据为止。

此代码显示的是如何实现长轮询

- (void) longPoll { 
//create an autorelease pool for the thread 
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 

//compose the request 
NSError* error = nil; 
NSURLResponse* response = nil; 
NSURL* requestUrl = [NSURL URLWithString:@"http://www.example.com/pollUrl"]; 
NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl]; 

//send the request (will block until a response comes back) 
NSData* responseData = [NSURLConnection sendSynchronousRequest:request 
         returningResponse:&response error:&error]; 

//pass the response on to the handler (can also check for errors here, if you want) 
[self performSelectorOnMainThread:@selector(dataReceived:) 
     withObject:responseData waitUntilDone:YES]; 

//clear the pool 
[pool drain]; 

//send the next poll request 
[self performSelectorInBackground:@selector(longPoll) withObject: nil]; 
} 

-(void) startPoll { 
    //not covered in this example: stopping the poll or ensuring that only 1 poll is active at any given time 
    [self performSelectorInBackground:@selector(longPoll) withObject: nil]; 
} 

-(void) dataReceived: (NSData*) theData { 
    //process the response here 
} 
+0

谢谢,我可以设置一个时间吗? – iqueqiorio 2015-03-17 03:50:10

+0

这可以异步完成吗? – iqueqiorio 2015-03-17 03:52:37

+0

我已经将网址更改为64.182.231.116/~spencerf/babson/babson_trim_menu.xml – iqueqiorio 2015-03-17 21:42:34

1

您的网址返回404未找到,所以你应该可以解决服务器的一部分

1

尝试增加超时时间间隔为您请求的示例:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 
[request setTimeoutInterval:60.0];//seconds