HTTPRequest binary data posting problem

You must Login before you can answer or comment on any questions.

I am very new in titanium and I have an annoying problem with posting binary data. I am developing mobile android application and I wanna record an audio file and then post this file via http request to a http server. I have recorded the audio wav file successfully. But when the time comes for posting this file by http request, the problem begins. In the titanium side, the size of the file is 75000 byte for example, on the other side --on my server-- I got a buffer with 82000 byte. When I compare the two sides' files with a hex editor the bytes of the server side is not identical of the original one. Therefore, the wav file on the server is corrupted,it does not play. Is there any suspect that the http request in the titanium-project will alter the bytes? May be cast to string? Because I faced the same problem when listening network traffic via WireShark. In WireShark there comes manipulated byte data from http.

Please help..

Here is my code ;

Client :

sendBtn.addEventListener('click', function(e) { var client=Titanium.Network.createHTTPClient();

var uploadFile = Titanium.Filesystem.getFile(sd_card_path,'myfile.ulaw');
var content = uploadFile.read();
var boundary = '----12345568790';

Ti.API.info('content = ' + content);
Ti.API.info('content.length = '+content.length);

client.open("POST","http://172.20.143.47:8080/MyProject/mediaHandler");
client.setRequestHeader("Content-type", "multipart/form-data; boundary=\"" + boundary + "\"");  
client.setRequestHeader("Connection", "close");
//client.setRequestHeader('Content-Type', 'audio/basic');
client.send(content);
client.onreadystatechange = function(){  
   if (this.readyState == 4){  
      alert(this.responseText);  
   }  
}

});

Server :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String header = request.getHeader("Content-Type"); System.out.println("header : "+header); ServletInputStream stream = request.getInputStream(); System.out.println(request.getContentLength()); byte b = (byte)stream.read(); String strFilePath = "C://Media//demo.ulaw"; File f = new File(strFilePath); FileOutputStream fos = new FileOutputStream(strFilePath); String length = ""; int contentLength = 0; boolean writeToFile = false; int i = 0; while (b!=-1){ i++; fos.write(b); System.out.print((char)b); b = (byte)stream.read(); } System.out.println("i : "+i); fos.close(); }

Your Answer

Think you can help? Login to answer this question!