java - How to handle a IOException by starting a new Activity in android -
i trying start main page activity when exception thrown. right when exception happens, app shuts down , starts main page. want seamlessly go main page app, not give user waring app shutting down. error gives me this:
d/issue with http:: ioexception 04-19 13:57:31.518 27520-27570/myapp w/system.err: java.io.filenotfoundexception: webpage 04-19 13:57:31.518 27520-27570/myapp w/system.err: @ com.android.okhttp.internal.huc.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:238) 04-19 13:57:31.518 27520-27570/myapp w/system.err: @ myapp.httpmanager.myhttpget.httpdownloaddata(myhttpget.java:79) 04-19 13:57:31.518 27520-27570/myapp w/system.err: @ myapp.httpmanager.myhttpget.doinbackground(myhttpget.java:53) 04-19 13:57:31.518 27520-27570/myapp w/system.err: @ myapp.httpmanager.myhttpget.doinbackground(myhttpget.java:31) 04-19 13:57:31.518 27520-27570/myapp w/system.err: @ android.os.asynctask$2.call(asynctask.java:295) 04-19 13:57:31.518 27520-27570/myapp w/system.err: @ java.util.concurrent.futuretask.run(futuretask.java:237) 04-19 13:57:31.519 27520-27570/myapp w/system.err: @ android.os.asynctask$serialexecutor$1.run(asynctask.java:234) 04-19 13:57:31.519 27520-27570/myapp w/system.err: @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1113) 04-19 13:57:31.519 27520-27570/myapp w/system.err: @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:588) 04-19 13:57:31.519 27520-27570/myapp w/system.err: @ java.lang.thread.run(thread.java:818) 04-19 13:57:31.538 27520-27520/myapp d/androidruntime: shutting down vm 04-19 13:57:31.594 27520-27520/myapp e/androidruntime: fatal exception: main process: myapp, pid: 27520 java.lang.nullpointerexception: attempt invoke virtual method 'boolean java.lang.string.contains(java.lang.charsequence)' on null object reference @ myapp.json.loginjsonparser.checkusername(loginjsonparser.java:41) @ myapp.loginpage$2.onclick(loginpage.java:128) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
this fine , dandy dev, want start new activity when happens. have code wise problem this.
private string httpdownloaddata(string myurl) { string respone = null; httpurlconnection urlconnection = null; try { url url = new url(myurl); //put in username , pssword parmas send url //this login if (username!=null) { authenticator.setdefault(new authenticator() { @override protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(username, password.tochararray()); } }); } urlconnection = (httpurlconnection)url.openconnection(); inputstream inputstream = urlconnection.getinputstream(); if(inputstream != null) { respone = streamtostring(inputstream); inputstream.close(); } }catch (ioexception ie) { //ie.printstacktrace(); log.d("issue http: " , "ioexception"); ie.printstacktrace(); intent newintent = new intent(activity.getapplicationcontext(), ecom.class); activity.startactivity(newintent); }finally { if(urlconnection != null) { urlconnection.disconnect(); } } return respone; }
thank this.
you cannot perform networking operation in ui thread, give networkonmainthreadexception. please use asynctask perform network , long running operation. can find samples , docs here
or use volley framework handle network operations
Comments
Post a Comment