Tuesday, 15 February 2011

java - I have to close app and open again for android file upload to work -



java - I have to close app and open again for android file upload to work -

i have android application records sound file , uploads api fingerprinting. app works when uploads first time , gets right response. however, on trying upload 1 time again request app seems reach server without uploaded file. strangely, when close app , open again, works well. however, want able uploads continuously without having close after every attempt.

here code:

public class mainactivity extends activity { class uploadfiletask extends asynctask<void, void, string> { @override protected string doinbackground(void... voids) { string responsestring = ""; httpresponse response = null; seek { //below api url string url = "http://1**.**.**.**:8000/api/tag/"; file track = new file(environment.getexternalstoragedirectory(), "mezzo.mp3"); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); multipartentitybuilder reqentity = multipartentitybuilder.create(); reqentity.setmode(httpmultipartmode.browser_compatible); filebody fb = new filebody(track); //inputstream inputstream = new ; //reqentity.addpart("track", fb); reqentity.addbinarybody("track", track); final httpentity myentity = reqentity.build(); httppost.setentity(myentity); log.i("request", string.valueof(myentity)); response = httpclient.execute(httppost); //process response responsestring = new basicresponsehandler().handleresponse(response); } grab (filenotfoundexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } { if (null!=response) { seek { httpentity httpentity = response.getentity(); if (null != httpentity) { httpentity.consumecontent(); } } grab (ioexception e) { e.printstacktrace(); } } } homecoming responsestring; } protected void onpostexecute(final string responsestring) { runonuithread(new thread() { public void run() { toast.maketext(getapplicationcontext(), responsestring, toast.length_long).show(); } }); }

what i'm doing wrong?

i not sure suspect http response might not consumed entirely.

can seek consuming response before homecoming function in block:

finally { if (null!=response) { seek { httpentity httpentity = response.getentity(); if (null != httpentity) { httpentity.consumecontent(); } } grab (ioexception e) { e.printstacktrace(); } } }

java android multipartform-data

No comments:

Post a Comment