2012-06-02 57 views
1

我正在開發一個iOS應用程序。我必須以編程方式將音頻文件發送到我們的服務器。我正在使用以下代碼將示例wav音頻文件發送到服務器。我們的服務器只接受符號字節數組格式的音頻文件進行接收。iPhone:錯誤上傳音頻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文章中發送字節數組?

回答

0

有幾個問題:首先 你是如何閱讀該文件將不會返回一個有效的WAV文件:

您應該使用[NSData的dataWithContentsOfURL:]讀取的字節數。你讀它們的方式會嘗試將字節轉換爲字符。這會破壞你的wav文件。

其次,POST正文的內容類型可能不正確。請看行:

[recordRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

音頻字節不會以x-www-form-urlencoded格式編碼。服務器更可能接受音頻/ wav或應用程序/八位字節流內容類型。請諮詢服務器開發人員,確切瞭解服務器的期望。另外,您可能想要查看名爲setHTTPBodyStream的NSMutableURLRequest方法:它可以讓您發送文件而不必將整個內容加載到內存中。

+0

好的謝謝,還有一個問題,是NSData無符號字節數組? – Stella

+0

是的,NSData是字節的集合。因此,當您將它們添加爲POST主體時,它們將作爲字節流添加到請求中。我強烈建議你使用setHTTPBodyStream:方法作爲流發送主體,而不是將它們加載到內存中。流方法將自動填充HTTP請求的內容長度頭,因此您不需要進行該計算。 –

+0

嗨,我問我的服務器端工程師,他表示帖子主體應該只包含文件,沒有其他的東西。所以,我刪除了其他東西,並嘗試使用setHttpBodyStream(),如你所建議的。請檢查上述問題的更新代碼。但是,它沒有做任何事情。已收到空的答覆。 – Stella

相關問題