更新:显然在 iOS 5 上,问题是“分块编码”,如果没有发送,一切正常。似乎在服务器上由于某种原因在 iOS 5 上传输永远不会结束(在 iOS 6 上一切正常)。有人有办法解决吗?
我正在使用 NSURLConnection,它在 iOS 6 和同一版本的模拟器上完美运行,但是在早期设备上测试时,我只得到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
从来没有
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
其中假设包含我的相关数据。
这是我使用过的所有函数的代码片段(我看到对于某些人来说删除一些委托函数解决了类似的问题,但在我的情况下我没有它们):
-(void)establishConnection{
NSURL *url;
url = .... // Here I've set my url - it's https
self.responseData = [[NSMutableData alloc] initWithLength:0] ;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:SERVER_RESPONSE_TIMEOUT];
[request setHTTPMethod:@"POST"];
// More settings here //
....
//Accept-Language: ENUS
[request addValue:@"ENUS" forHTTPHeaderField:@"Accept-Language"];
// "Accept-Topic: Dictation"
[request addValue:@"Dictation" forHTTPHeaderField:@"Accept-Topic"];
// "Accept: text/plain"
[request addValue:@"text/plain" forHTTPHeaderField:@"Accept"];
//"Transfer-Encoding: chunked"
[request addValue:@"chunked" forHTTPHeaderField:@"Transfer-Encoding"];
NSMutableData *postBody = [NSMutableData data];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [NSString stringWithFormat:@"%@",[paths objectAtIndex:0]]; // Get sound directory
NSData *soundData = [NSData dataWithContentsOfFile: [NSString stringWithFormat:@"%@/%@",documentsDirectory, @"rec.wav"]];
[postBody appendData:soundData];
[postBody appendData:[@"\r\n" dataUsingEncoding: NSUTF8StringEncoding]];
// final boundary
//[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post
[request setHTTPBody:postBody];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// You may have received an HTTP 200 here, or not...
NSLog(@"didReceiveResponse");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"This is my first chunk %@", aStr);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connectionV {
connectionV = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Something went wrong...");
}
请帮助我找不到我做错了什么。