2017-03-21 5 views
0

서버에서 데이터를 가져 오는 데 GET을 사용하고 있습니다. 진도 표시 줄을 구현하고 싶습니다. didReceiveResponse 대리자 메서드에서 expectedContentLength를 캡처하려고했습니다. 어떻게 해결할 수 있습니까? NSURLConnection, didReceiveResponse expectedContentLength get-

NSString* serverUrl = @"https:testURL"; 

receivedData = [[NSMutableData alloc] initWithLength:0]; 

NSURL* url =[NSURL URLWithString:[serverUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 

[theRequest setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];**(I tried it after seeing other solutions over Web)** 

[NSURLConnection connectionWithRequest:theRequest delegate:self]; 

은 ...

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    [receivedData setLength:0]; 
    expectedBytes = [response expectedContentLength]; 
} 
+1

모든 서버가 (또는 더 정확하게는 모든 요청이 아닌) 'Content-Length' 헤더를 반환하지는 않습니다. 스크립트에서이를 생략하는 경우는 매우 빈번합니다. 서버가 자신의 제어하에 있다면 적절한 'Content-Length' 헤더를 보내야합니다. – jcaron

+0

우리는 allHeaders에서 Content-Length를 얻지 못하고있다. – kiri

+0

'gzip; q = 0'의'Accept-encoding'이 잘 작동한다고보고 한 일부 사람들이 (http://stackoverflow.com/a/17444913/1271826). 기타 [제안] (http://stackoverflow.com/a/11204338/1271826) 길이가 0 인 문자열. 귀하의 질문에 나열된'정체성 '을 성공적으로 사용했습니다. – Rob

답변

0
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    _contentLength = [response expectedContentLength]; 

} 
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
_receiveLength += data.length; 
} 

당신은 그렇게 생각하십니까?