2014-11-14 61 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