2011-07-04 111 views
1

我嘗試用命令以檢索某些URL數據:NSURLConnection的失敗 - 1003

-(NSMutableData *) callUrl: (NSString *)url withData:(NSMutableDictionary *)data delegate:(id) delegate { 

    NSURL *executeUrl = [NSURL URLWithString:<string>]; 
    NSURLRequest *request = [NSURLRequest requestWithURL: executeUrl 
          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
          timeoutInterval:60]; 

    NSMutableData *receivedData = nil; 

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:delegate]; 
    if (theConnection) { 
     receivedData = [[NSMutableData data] retain]; 
    } else { 
     @throw @"Connection error"; 
    } 

    return receivedData; 
} 

在委託(包括後connectionDidFinish和connectionDidFailWithError)我做的:

//some uninvasive alerts 
// release the connection, and the data object 
[connection release]; 
[receivedData release]; 

問題是,當我提供壞網址我得到了適當的錯誤 - 這是很好的一部分 - 但是我想執行第二個網址 - 很好,我有1003錯誤 - NSURLErrorCannotFindHost。

大約1-2分鐘後,我成功地調用URL並獲取數據。 我懷疑有些超時和端口業務,但在NSURLRequest中更改超時並不會改變事情。

UPDATE

事實證明 - 管理員必須通過WiFi網絡達到DNS服務器的一些問題。代碼很好。感謝您的迴應。 如果有一些類似的問題:嘗試IP地址而不是主機名。

+0

你是怎麼修復它的?你嘗試使用IP地址嗎?我也面臨同樣的問題。請建議。 –

回答

0

在進行任何新的連接呼叫之前取消上一個連接。 使用

[self.connection cancel]; 
+0

我在釋放它之前嘗試取消連接。不起作用。 – vitotao

1

從蘋果iOS開發者文檔,1003錯誤是指當一個URL中的主機名不能得到解決。爲避免在無線局域網中出現DNS故障,DNS過載情況超時,最好從主機名解析IP以供後續使用,或者直接硬編碼IP地址,如果您不打算稍後轉移主機。

蘋果文檔:

URL Loading System Error Codes 

這些值返回爲與域「NSURLErrorDomain」一個NSError對象的錯誤代碼屬性。

enum 
{ 
    NSURLErrorBadURL = -1000, 
    NSURLErrorTimedOut = -1001, 
    NSURLErrorUnsupportedURL = -1002, 
    NSURLErrorCannotFindHost = -1003,//**** 
    NSURLErrorCannotConnectToHost = -1004, 
    NSURLErrorDataLengthExceedsMaximum = -1103, 
    NSURLErrorNetworkConnectionLost = -1005, 
    NSURLErrorDNSLookupFailed = -1006, 
    ... 
    } 

1003 NSURLErrorCannotFindHost 
Returned when the host name for a URL cannot be resolved. 
Available in iOS 2.0 and later. 
Declared in NSURLError.h. 
1

我做了兩件事情來解決這個問題:

  1. 我開始我的NSURLConnection的

    [NSURLConnection cancelPreviousPerformRequestsWithTarget:self];

  2. 我改變了我的DNS到8.8.8.8在WiFi設置使用此之前這是谷歌的公共DNS服務器。

不知道哪一個固定它,但問題已解決。

相關問題