我有一个网址,我试图从中获取XML。
现在在我的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: <>
不确定为什么会发生这种情况,我无法区分它何时起作用以及何时发生变化?所以我不知道如何解决这个问题?
我想要的是每次获取数据吗?
感谢adavence的帮助。
答案 0 :(得分:2)
尝试增加请求的超时间隔:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeoutInterval:60.0];//seconds
答案 1 :(得分:1)
你的代码很好。它是给你麻烦的服务器。
我重新加载了你提到过20次的页面。它成功加载了13次。它返回7次没有错误,但也是一个空响应体。
您可以做的只是在您的应用中检查此情况,然后再次运行请求再试一次。
或与服务器所有者交谈,了解他们是否可以提高此服务的可靠性。
答案 2 :(得分:1)
在设备上运行代码,当没有收到数据时,错误消息为 null 或错误域= NSURLErrorDomain Code = -1005“网络连接丢失。”。
关于后一个错误,您可以看到AFNetworking/issues/2314,下面是从帖子中提取的评论。
当iOS客户端收到带有Keep-Alive标头的HTTP响应时,它 保持这个连接以后重复使用(应该如此),但它保留它 超过Keep-Alive标头的超时参数然后 当第二个请求到来时,它会尝试重新使用具有的连接 已被服务器删除。
您也可以参考解决方案here,如果您无法更改服务器,我建议您在错误代码为-1005或收到的数据长度为0时重试请求。
答案 3 :(得分:1)
也许Long-Polling会帮助你。我尝试时,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
}
答案 4 :(得分:1)
您的网址返回404 Not Found,因此您应修复服务器部分