i trying upload file using asynctask, application running fine until click on upload button (which defined in uploadactivity.java) app stops. here logcat.
**note:**i have mentioned line numbers of given exceptions logcat.
fatal exception: asynctask #1 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:299) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) @ java.util.concurrent.futuretask.setexception(futuretask.java:219) @ java.util.concurrent.futuretask.run(futuretask.java:239) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) @ java.lang.thread.run(thread.java:841) caused by: java.lang.noclassdeffounderror: org.apache.commons.io.output.bytearrayoutputstream @ org.apache.http.entity.mime.httpmultipart.gettotallength(httpmultipart.java:219) @ org.apache.http.entity.mime.multipartentity.getcontentlength(multipartentity.java:150) @ com.example.imtiaz.recognizer.uploadactivity$uploadfiletoserver.uploadfile(uploadactivity.java:129) @ com.example.imtiaz.recognizer.uploadactivity$uploadfiletoserver.doinbackground(uploadactivity.java:108) @ com.example.imtiaz.recognizer.uploadactivity$uploadfiletoserver.doinbackground(uploadactivity.java:90) @ android.os.asynctask$2.call(asynctask.java:287) @ java.util.concurrent.futuretask.run(futuretask.java:234) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) @ java.lang.thread.run(thread.java:841)
here uploadactivity.java code.
public class uploadactivity extends appcompatactivity { private static final string tag = mainactivity.class.getsimplename(); private progressbar progressbar; private string filepath = null; private imageview imgpreview; private button btnupload; long totalsize = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_upload); btnupload = (button) findviewbyid(r.id.btnupload); progressbar = (progressbar) findviewbyid(r.id.progressbar); imgpreview = (imageview) findviewbyid(r.id.imageview); // receiving data previous activity intent = getintent(); filepath = i.getstringextra("filepath"); // boolean flag identify media type, boolean isimage = i.getbooleanextra("isimage", true); if (filepath != null) { // displaying image on screen previewmedia(isimage); } else { toast.maketext(getapplicationcontext(), filepath + "sorry, file path missing!", toast.length_long).show(); } //creating eventlistener. btnupload.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // uploading file server new uploadfiletoserver().execute(); } }); } private void previewmedia(boolean isimage) { //checking mediatype image or not if (isimage) { imgpreview.setvisibility(view.visible); //reducing image size. bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; //bitmaping image . final bitmap bitmap = bitmapfactory.decodefile(filepath, options); imgpreview.setimagebitmap(bitmap); } } //else give message media type not image. private class uploadfiletoserver extends asynctask<void, integer, string>//line 90 { @override protected void onpreexecute() { progressbar.setprogress(0); super.onpreexecute(); } @override protected void onprogressupdate(integer... progress) { //set view true. progressbar.setvisibility(view.visible); //progressbar value. progressbar.setprogress(progress[0]); } @override protected string doinbackground(void... params) { //do background task (uploadfile). return uploadfile();//line 108 } @suppresswarnings("deprecation") private string uploadfile() { string responsestring = null; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(config.file_upload_url); try { androidmultipartentity entity = new androidmultipartentity( new progresslistener() { @override public void transferred(long num) { publishprogress((int) (num / (float) totalsize) * 100); } }); file sourcefile = new file(filepath); //adding file data http body. entity.addpart("image", new filebody(sourcefile)); totalsize = entity.getcontentlength();//line 129 httppost.setentity(entity); //making server calls. httpresponse response = httpclient.execute(httppost); httpentity r_entity = response.getentity(); int statuscode = response.getstatusline().getstatuscode(); if (statuscode == 200) { //server response. responsestring = entityutils.tostring(r_entity); } else { responsestring = "error occurred status: " + statuscode; } } catch (clientprotocolexception e) { responsestring = e.tostring(); } catch (ioexception e) { responsestring = e.tostring(); } return responsestring; } @override protected void onpostexecute(string result) { log.e(tag, "response server : " + result); //showing response. showalert(result); super.onpostexecute(result); } } private void showalert(string message) { alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage(message).settitle("response servers") .setcancelable(false) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // nothing } }); alertdialog alert = builder.create(); alert.show(); } }
dependencies
dependencies { compile filetree(include: ['*.jar'], dir: 'libs') testcompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.2.0' compile files('libs/httpclient-4.0.jar') compile files('libs/httpcore-4.0-alpha5.jar') compile files('libs/apache-mime4j-0.3.jar') compile files('libs/commons-io-2.5.jar')}
you're missing dependency:
compile group: 'commons-io', name: 'commons-io', version: '2.5'
Comments
Post a Comment