android - AsyncTask Calling itself multiple Times -
i have asynctask working except calls doinbackground
multiple time.
i'm calling broadcastreceiver
asynctask
broadcastreceiver
calls service
service executes background works calls broadcastreceiver 3 times why ? ideas?
hint :
onreceive
inlogcat
refersonreceive
method inmybroadcastreceiver
,oncommandstart
refersmyservice
uploadfileasync.java:
public class uploadfileasync extends asynctask<string, integer, string> { private int serverresponsecode = 0; private string sourcefileuri, link, spec, username, title, abstract_, desc; private intent intent; private context ctx; private notificationcompat.builder mbuilder; private notificationmanager mnotifymanager; public uploadfileasync(intent intent, string sourcefileuri, context ctx) { this.intent = intent; this.sourcefileuri = sourcefileuri; this.ctx = ctx; } private void prepareparams() { this.title = intent.getextras().getstring("title"); this.username = intent.getextras().getstring("username"); this.abstract_ = intent.getextras().getstring("abstract"); this.link = intent.getextras().getstring("link"); this.spec = intent.getextras().getstring("spec"); this.desc = intent.getextras().getstring("desc"); } @override protected string doinbackground(string... params) { log.e("doinbackground", "==========================="); try { httpurlconnection conn = null; dataoutputstream dos = null; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; file sourcefile = new file(sourcefileuri); if (sourcefile.isfile()) { try { string uploadserveruri = constants.upload_file_to_server; fileinputstream fileinputstream = new fileinputstream( sourcefile); url url = new url(uploadserveruri); conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); conn.setdooutput(true); conn.setusecaches(false); conn.setrequestmethod("post"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("enctype", "multipart/form-data"); conn.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); conn.setrequestproperty("masharee3", sourcefileuri); dos = new dataoutputstream(conn.getoutputstream()); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name=\"masharee3\";filename=\"" + sourcefileuri + "\"" + lineend); dos.writebytes(lineend); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { dos.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math .min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend); serverresponsecode = conn.getresponsecode(); string serverresponsemessage = conn .getresponsemessage(); if (serverresponsecode == 200) { log.e("server response = 200", " "+serverresponsemessage); mbuilder.setcontenttext(ctx.getresources().getstring(r.string.publish_completed)) .setprogress(0, 0, false); mnotifymanager.notify(constants.not_id, mbuilder.build()); intent = new intent() .putextra("username", username) .putextra("title", title) .putextra("spec", spec) .putextra("link", link) .putextra("desc", desc) .putextra("abstract", abstract_); i.setaction("com.omaralmrsomi.masharee3app.upload_complete_notification"); ctx.getapplicationcontext().sendbroadcast(i); } else { mbuilder.setcontenttext(ctx.getresources().getstring(r.string.publish_failed)) .setprogress(0, 0, false); mnotifymanager.notify(constants.not_id, mbuilder.build()); } fileinputstream.close(); dos.flush(); dos.close(); } catch (exception e) { e.printstacktrace(); } } } catch (exception ex) { ex.printstacktrace(); } return string.valueof(serverresponsecode); } @override protected void onpostexecute(string result) { log.e("onpostexecute", "==========================="); } @override protected void onpreexecute() { log.e("onpreexecute", "============================"); prepareparams(); mnotifymanager = (notificationmanager) ctx.getsystemservice(context.notification_service); mbuilder = new notificationcompat.builder(ctx); mbuilder.setcontenttitle(ctx.getresources().getstring(r.string.publishing_project)) .setcontenttext(ctx.getresources().getstring(r.string.publishing)) .setsmallicon(r.drawable.ic_stat); mbuilder.setprogress(0, 0, true); mnotifymanager.notify(constants.not_id, mbuilder.build()); } }
my logcat
09-13 22:56:40.271 9505-9505/com.omaralmrsomi.masharee3app e/onpreexecute: ============================ 09-13 22:56:40.355 9505-9653/com.omaralmrsomi.masharee3app e/doinbackground: =========================== 09-13 22:56:41.064 9505-9653/com.omaralmrsomi.masharee3app e/server response = 200: ok 09-13 22:56:41.078 9505-9505/com.omaralmrsomi.masharee3app e/onrecevie: ======--========= 09-13 22:56:41.090 9505-9505/com.omaralmrsomi.masharee3app e/onrecevie: ======--========= 09-13 22:56:41.102 9505-9505/com.omaralmrsomi.masharee3app e/onrecevie: ======--========= 09-13 22:56:41.110 9505-9505/com.omaralmrsomi.masharee3app e/oncommandstart: ============ 09-13 22:56:41.110 9505-9505/com.omaralmrsomi.masharee3app e/getdata not null: ===-=- 09-13 22:56:41.110 9505-9505/com.omaralmrsomi.masharee3app e/uploadtoserver: =-=-= 09-13 22:56:41.124 9505-9505/com.omaralmrsomi.masharee3app e/oncommandstart: ============ 09-13 22:56:41.124 9505-9505/com.omaralmrsomi.masharee3app e/getdata not null: ===-=- 09-13 22:56:41.124 9505-9505/com.omaralmrsomi.masharee3app e/uploadtoserver: =-=-= 09-13 22:56:41.134 9505-9505/com.omaralmrsomi.masharee3app e/oncommandstart: ============ 09-13 22:56:41.134 9505-9505/com.omaralmrsomi.masharee3app e/getdata not null: ===-=- 09-13 22:56:41.134 9505-9505/com.omaralmrsomi.masharee3app e/uploadtoserver: =-=-= 09-13 22:56:41.164 9505-9505/com.omaralmrsomi.masharee3app e/onpostexecute: ===========================
update :
this how call asynctask :
create_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkconnection chk = new checkconnection(getapplicationcontext(), newpost.this); if (chk.testconnection()) { if ((!inputtitle.gettext().tostring().equals("")) && (!inputabstract.gettext().tostring().equals("")) && (!inputdesc.gettext().tostring().equals(""))) { if (state == 1) { pdfpathholder = filepath.getpath(newpost.this, uri); link = constants.server_link + "" + (pdfpathholder != null ? pdfpathholder.substring(pdfpathholder.lastindexof("/") + 1) : null); username = username.gettext().tostring(); title = inputtitle.gettext().tostring(); desc = inputdesc.gettext().tostring(); abstract_ = inputabstract.gettext().tostring(); new_spec = check(); intent = new intent() .putextra("username", username) .putextra("title", title) .putextra("spec", new_spec) .putextra("link", link) .putextra("desc", desc) .putextra("abstract", abstract_); new uploadfileasync(i, pdfpathholder, newpost.this).execute(""); } else { toast.maketext(newpost.this, please_select_afile, toast.length_long).show(); } } else { toast.maketext(newpost.this, one, toast.length_long).show(); } } else { toast.maketext(newpost.this, bad_internet, toast.length_short).show(); } } });
Comments
Post a Comment