我正在开发一个 iOS 应用程序。我必须以编程方式将音频文件发送到我们的服务器。我正在使用以下代码将示例 wav 音频文件发送到服务器。我们的服务器只接受有符号字节数组格式的音频文件进行接收。
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"Crash" ofType:@"wav"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSLog(@"wavbundlepath: %@",wavbundlepath);
NSData *bytes = [wavbundlepath dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"bytes: %@",bytes);
NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", @"http://www.mywebserver/api/UploadFile?Name="];
[urlstr appendString:@"crash"];
[urlstr appendFormat:@"&MemberID=%d", 0];
[urlstr appendFormat:@"&Type=%@",@"Recording"];
[urlstr appendFormat:@"&client=%@",@"ios"];
NSLog(@"urlstr.......%@",urlstr);
NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
[recordRequest setURL:[NSURL URLWithString:urlstr]];
[recordRequest setHTTPMethod:@"POST"];
[recordRequest setValue:recordPostLength forHTTPHeaderField:@"Content-Length"];
[recordRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[recordRequest setHTTPBody:bytes];
NSURLResponse *recordResponse;
NSError *recordError;
NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
NSLog(@"recordResp:%@", recordResp);
但问题是,总是收到“输入字符串不正确”。响应错误。
我不太了解将音频字节上传到服务器。有人可以检查此代码并告诉我这是用于将 wav 音频字节数组上传到服务器的有效代码吗?
谢谢你。
更新代码
我尝试了下面的代码,因为我的服务器端工程师说没有任何其他的东西发布正文,它工作正常并得到了积极的回应。但是,服务器无法使用我正在发送的格式 NSData 字节(32 位元素),因为服务器端实现了仅接收字节数组或仅接收有符号字节数据格式。
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"Temp" ofType:@"wav"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSLog(@"wavbundlepath: %@",wavbundlepath);
NSData *bytes=[NSData dataWithContentsOfURL:urlPath];
NSLog(@"bytes: %@",bytes);
NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", @"http://www.myserver.com/api/UploadFile?Name="];
[urlstr appendString:@"Temp"];
[urlstr appendFormat:@"&MemberID=%d", 0];
[urlstr appendFormat:@"&Type=%@",@"Recording"];
[urlstr appendFormat:@"&client=%@",@"ios"];
NSLog(@"urlstr.......%@",urlstr);
NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
[recordRequest setURL:[NSURL URLWithString:urlstr]];
NSInputStream *dataStream = [NSInputStream inputStreamWithData:bytes];
[recordRequest setHTTPBodyStream:dataStream];
[recordRequest setHTTPMethod:@"POST"];
NSURLResponse *recordResponse;
NSError *recordError;
NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
NSLog(@"recordResp:%@", recordResp);
recordResponceJson = [recordResp JSONValue];
NSLog(@"recordResponceJson = %@",recordResponceJson);
recId = [recordResponceJson valueForKey:@"ID"];
NSLog(@"recId....%@", recId);
有人可以指导我,我如何在这个 http 帖子中发送字节数组?