in asynctask,either progress bar working or file uploading successfully. comment2 line upload file when write before comment1 line progress bar not working. if write comment2 after comment1 face error code 500(syntax error) , uploading failed progress bar works properly.
private class asynccaller extends asynctask<string, integer, string> { int bytesread, bytesavailable, buffersize = 1024,progress; byte[] buffer; @override protected void onpreexecute() { super.onpreexecute(); mprogressdialog = new progressdialog(getactivity()); mprogressdialog.setmessage("uploading file.."); mprogressdialog.setprogressstyle(progressdialog.style_horizontal); mprogressdialog.setindeterminate(false); mprogressdialog.setcancelable(false); mprogressdialog.setprogress(0); mprogressdialog.setmax(100); mprogressdialog.show(); } @override protected string doinbackground(string... params) { // todo auto-generated method stub // connection(); ftpclient con = null; try { con = new ftpclient(); con.connect(ftp_host); if (con.login(ftp_user, ftp_pass)) { con.enterlocalactivemode(); // important! con.setfiletype(ftp.binary_file_type); con.changeworkingdirectory("/uploads/school-staging/files/"); string data = filepath; final dataoutputstream out = new dataoutputstream(con.getoutputstream()); bufferedinputstream in = new bufferedinputstream(new fileinputstream(data)); int buffersize=1024; byte[] buffer = new byte[buffersize]; // read file bytesread = in.read(buffer, 0, buffersize);//comment 1 progress=0; system.out.println("byte read="+bytesread); while (bytesread > 0) { progress+=bytesread; system.out.println("progress="+progress); out.write(buffer, 0, bytesread); bytesavailable = in.available(); publishprogress((int)((progress*100)/(file.length()))); buffersize = math.min(bytesavailable, buffersize); bytesread = in.read(buffer, 0, buffersize); } boolean result = con.storefile(filename, in);//comment 2 int code= con.getreplycode(); system.out.println("code="+code); in.close(); publishprogress(100); if (result) log.v("upload result", "succeeded"); system.out.println("result="+result); con.logout(); con.disconnect(); } } catch (exception e) { e.printstacktrace(); } return "ok"; } @override public void onprogressupdate(integer... values) { super.onprogressupdate(values); mprogressdialog.setprogress(values[0]); }//end of onprogressupdate @override protected void onpostexecute(string result) { super.onpostexecute(result); connection(); mprogressdialog.dismiss(); }
i've faced similar problem. reason seems ui code in onpreexecute() doesnt render android system, , code in doinbackground() occupies cpu. can dismiss dialog in onpostexecute() though...
you'd have show progress dialog before creating asynccaller, pass progress dialog asynccaller , can dismiss in onpostexecute().
progressdialog pd = new progressdialog(getactivity()); pd.settitle("title string"); pd.setindeterminate(true); pd.setprogressstyle(progressdialog.style_horizontal); pd.show(); new asynccaller(getactivity(), pd).execute(params); in onpostexecute(): if (pd != null && pd.isshowing()) pd.dismiss();
Comments
Post a Comment