android - Can't create handler inside thread that has not called Looper.prepare() when using AsyncTask -
i have following code in app should update app. host apk on private server , when put new version want code run , update app.
i know play store , use mechanism of our customers. there few customers refuse have background data switched on due data usage. these customer can't use play store, want graceful update method our private server.
i have read posts on , general feeling i'm calling should called on ui thread. if i'm making web call, must on bg thread.
any ideas why i'm getting error message?
thanks in advance
matt
string url = "http://cfweb.yourofficeanywhere.co.uk/entryactivityv33.apk"; string[] updateparam = new string[] {url}; asyncupdateapp asua = new asyncupdateapp(); asua.execute(updateparam);
.
private class asyncupdateapp extends asynctask<string, string, string>{ @override protected string doinbackground(string... params) { string result = updateapp(params[0]); return result; } @override protected void onpostexecute(string result) { super.onpostexecute(result); if (result.equalsignorecase("ok")){ intent intent = new intent(intent.action_view); intent.setdataandtype(uri.fromfile(new file(environment.getexternalstoragedirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive"); intent.setflags(intent.flag_activity_new_task); startactivity(intent); }else{ log.e(tag, "result odf updateapp = error"); } } } public string updateapp(string apkurl){ try { url url = new url(apkurl); httpurlconnection c = (httpurlconnection) url.openconnection(); c.setrequestmethod("get"); c.setdooutput(true); c.connect(); string path = environment.getexternalstoragedirectory() + "/download/"; file file = new file(path); file.mkdirs(); file outputfile = new file(file, "app.apk"); fileoutputstream fos = new fileoutputstream(outputfile); inputstream = c.getinputstream(); byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = is.read(buffer)) != -1) { fos.write(buffer, 0, len1); } fos.close(); is.close();//till here, works fine - .apk download sdcard in download file return "ok"; } catch (ioexception e) { toast.maketext(getapplicationcontext(), "update error!", toast.length_long).show(); return "error"; } } //end of updateapp
.
09-10 14:31:04.975: e/androidruntime(606): fatal exception: asynctask #1 09-10 14:31:04.975: e/androidruntime(606): process: com.carefreegroup.rr3, pid: 606 09-10 14:31:04.975: e/androidruntime(606): java.lang.runtimeexception: error occured while executing doinbackground() 09-10 14:31:04.975: e/androidruntime(606): @ android.os.asynctask$3.done(asynctask.java:300) 09-10 14:31:04.975: e/androidruntime(606): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 09-10 14:31:04.975: e/androidruntime(606): @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 09-10 14:31:04.975: e/androidruntime(606): @ java.util.concurrent.futuretask.run(futuretask.java:242) 09-10 14:31:04.975: e/androidruntime(606): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 09-10 14:31:04.975: e/androidruntime(606): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 09-10 14:31:04.975: e/androidruntime(606): @ java.lang.thread.run(thread.java:811) 09-10 14:31:04.975: e/androidruntime(606): caused by: java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() 09-10 14:31:04.975: e/androidruntime(606): @ android.os.handler.<init>(handler.java:200) 09-10 14:31:04.975: e/androidruntime(606): @ android.os.handler.<init>(handler.java:114) 09-10 14:31:04.975: e/androidruntime(606): @ android.widget.toast$tn.<init>(toast.java:327) 09-10 14:31:04.975: e/androidruntime(606): @ android.widget.toast.<init>(toast.java:92) 09-10 14:31:04.975: e/androidruntime(606): @ android.widget.toast.maketext(toast.java:241) 09-10 14:31:04.975: e/androidruntime(606): @ com.carefreegroup.rr3.nfcscanneractivity.updateapp(nfcscanneractivity.java:6382) 09-10 14:31:04.975: e/androidruntime(606): @ com.carefreegroup.rr3.nfcscanneractivity$asyncupdateapp.doinbackground(nfcscanneractivity.java:6320) 09-10 14:31:04.975: e/androidruntime(606): @ com.carefreegroup.rr3.nfcscanneractivity$asyncupdateapp.doinbackground(nfcscanneractivity.java:1) 09-10 14:31:04.975: e/androidruntime(606): @ android.os.asynctask$2.call(asynctask.java:288) 09-10 14:31:04.975: e/androidruntime(606): @ java.util.concurrent.futuretask.run(futuretask.java:237) 09-10 14:31:04.975: e/androidruntime(606): ... 3 more
this block of code throws ioexception:
try { url url = new url(apkurl); httpurlconnection c = (httpurlconnection) url.openconnection(); c.setrequestmethod("get"); c.setdooutput(true); c.connect(); string path = environment.getexternalstoragedirectory() + "/download/"; file file = new file(path); file.mkdirs(); file outputfile = new file(file, "app.apk"); fileoutputstream fos = new fileoutputstream(outputfile); inputstream = c.getinputstream(); byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = is.read(buffer)) != -1) { fos.write(buffer, 0, len1); } fos.close(); is.close();//till here, works fine - .apk download sdcard in download file return "ok"; }
and in catch block can not run ui operation in doinbackground must remove line catch:
toast.maketext(getapplicationcontext(), "update error!", toast.length_long).show();
Comments
Post a Comment