2012-09-02 74 views
3

我試圖在NSURLConnection中使用MBProgressHUD。帶NSURLConnection的MBProgressHUD

在MBProgressHUD的示範項目的例子報道:

- (IBAction)showURL:(id)sender { 
    NSURL *URL = [NSURL URLWithString:@"https://github.com/matej/MBProgressHUD/zipball/master"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [connection start]; 
    [connection release]; 

    HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain]; 
    HUD.delegate = self; 
} 



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    expectedLength = [response expectedContentLength]; 
    currentLength = 0; 
    HUD.mode = MBProgressHUDModeDeterminate; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    currentLength += [data length]; 
    HUD.progress = currentLength/(float)expectedLength; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease]; 
    HUD.mode = MBProgressHUDModeCustomView; 
    [HUD hide:YES afterDelay:2]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [HUD hide:YES]; 
} 

運行它時,HUD確定模式旋轉的罰款。

我試圖實現這一點,但這裏

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
     currentLength += [data length]; 
     HUD.progress = currentLength/(float)expectedLength; 
    } 

圈是空的,沒有填充。

我不知道它是否取決於請求的網址的維度。

我請求讓我從我的網站下載的plist(〜80 KB),但圈內一直是空的,控制檯報告

<Error>: void CGPathAddArc(CGPath*, const CGAffineTransform*, CGFloat, CGFloat, CGFloat, CGFloat, CGFloat, bool): invalid value for start or end angle. 

我甚至試圖做到這樣:

float progress = 0.0f; 
    while (progress < 1.0f) { 
     progress += 0.01f; 
     HUD.progress = progress; 
    } 

但是現在這個圈子已經滿了,並沒有做任何動畫。

我認爲這取決於請求的網址的維度,但我不太確定,有誰知道如何解決這個問題嗎?

回答

5

我的問題解決了這種方式,從切換到NSURLRequestNSMutableURLRequest值沒有設置編碼(以前採用gzip)

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL]; 
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"]; 
+0

這確實可以解決問題,但爲什麼?你能解釋爲什麼這會導致服務器返回內容長度,否則它不會? –

+0

沒有在我的情況下工作....雖然upvoted你的答案.... – TheMall

+1

與'NSURLSession'相同的問題,它爲totalexpectedbytes返回'NSURLSessionTransferSizeUnknown'(-1)作爲響應下載。這種情況很可能發生在服務器返回「gzip」ed響應時,您的回答使我朝正確的方向發展。爲「Accept-Encoding」設置空白將告訴服務器客戶端未準備好接受任何特定格式(如「gzip」)的響應。所以服務器簡單地繞過「gzip」ping響應流並返回原始字節。幸運的是'NSURLSession'(和'NSURLconnection')可以得到預期的no。來自它的字節。 –

5

您應該在didReceiveResponse檢查[response expectedContentLength]的值。

http服務器可以省略「Content-Length」標頭,而使用「Transfer-Encoding:chunked」。在這種情況下,內容長度不是先驗已知的,並且[response expectedContentLength]返回NSURLResponseUnknownLength(它是-1)`。

我可以想象,將HUD.progress設置爲負值會導致控制檯消息的CGPathAddArc

根據文檔,它也可能發生累積currentLength變得比預期的響應長度大,所以你也應該檢查。

+0

你是對的。值是-1。有什麼我可以解決這個問題嗎? – Phillip

+0

@Pheel:在客戶端沒有什麼可以做的。如果服務器使用「Transfer-Encoding:chunked」,那麼你只是不知道預計有多少字節。 –

+0

哦,謝謝,我明白了。 – Phillip