getting FATAL EXCEPTION in MAIN when doing a callback,asyntask android -
after reading on android asynctask sending callbacks ui goal :
dialog box show when waiting authentication server , long process done, call invoked , dialog box dismissed.
my structure ( asyntask , log in activity separated classes )
- log in activity
- class extends aysnctask
- a call interface.
partial of codes :
public class spalshscreenactivity extends activity implements logincallback{ @override public void onwindowfocuschanged(boolean hasfocus) { super.onwindowfocuschanged(hasfocus); if (hasfocus) { loginbutton = (button) findviewbyid(r.id.btnlogin); loginbutton.setbackgrounddrawable(getresources() .getdrawable(r.drawable.button)); loginbutton .setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dialog = new progressdialog(spalshscreenactivity.this); dialog.setmessage("waiting......"); dialog.show(); service client = new service(new spalshscreenactivity(), email.gettext().tostring(), password.gettext().tostring()); string loginurl = "my url goes here"; client.execute(loginurl); } }); } } @override public void successfullylogin() { log.d("splash","successfullylogin"); dialog.dismiss(); // **<--------crash @ here** } @override public void failedtologin() { } }
service.java :
public class service extends asynctask<string, integer, string> { private logincallback callback; string username; string password; public service(logincallback callback, string username, string password) { this.callback = callback; this.username = username; this.password = password; } // handling ssl connection protected string doinbackground(string... arg) { // validation , accept type of self signed certificates simplesslsocketfactory simplesslfactory; string loginurl = arg[0]; try { simplesslfactory = new simplesslsocketfactory(null); simplesslfactory .sethostnameverifier((x509hostnameverifier) hostnameverifier); // enable http parameters params = new basichttpparams(); httpprotocolparams.setversion(params, httpversion.http_1_1); httpprotocolparams.setcontentcharset(params, http.utf_8); // register http , https protocols. https, register our // custom ssl factory object. schemeregistry registry = new schemeregistry(); registry.register(new scheme("http", plainsocketfactory .getsocketfactory(), 80)); registry.register(new scheme("https", simplesslfactory, 443)); // create new connection manager using newly created registry // , create new http client // using connection manager ccm = new threadsafeclientconnmanager(params, registry); } catch (keymanagementexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (unrecoverablekeyexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (nosuchalgorithmexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (keystoreexception e1) { // todo auto-generated catch block e1.printstacktrace(); } // todo auto-generated method stub httpclient client = new defaulthttpclient(ccm, params); list<namevaluepair> namevaluepair = new arraylist<namevaluepair>(2); namevaluepair.add(new basicnamevaluepair("user_session[email]", "lenanguyen@hotmail.ca")); namevaluepair.add(new basicnamevaluepair("user_session[password]","2congato")); // create http method httppost login = new httppost(loginurl); try { login.setentity(new urlencodedformentity(namevaluepair)); } catch (unsupportedencodingexception e1) { // todo auto-generated catch block e1.printstacktrace(); } // fire request try { httpresponse response = client.execute(login); httpentity entity = response.getentity(); inputstream = entity.getcontent(); this.result = convertstreamtostring(is); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); log.d("clientprotocolexception ", e.tostring()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); log.d("ioexception ", e.tostring()); } return result; } // called after doinbackground finishes protected void onpostexecute(string result) { log.e("result, yay!", this.result); this.callback.successfullylogin(); } }
and call interface
public interface logincallback { void successfullylogin (); void failedtologin(); }
however after dialog shows few seconds, app crashing , fatal exception in main
thrown in cat log. ideas guys.
ps :
log cat here
change
service client = new service(new spalshscreenactivity(), email.gettext().tostring(), password.gettext().tostring());
into
service client = new service(spalshscreenactivity.this, email.gettext().tostring(), password.gettext().tostring());
and
public flipgiveclient(logincallback callback, string username, string password) { this.callback = callback; this.username = username; this.password = password; }
into
public flipgiveclient(activity activity, string username, string password) { this.callback = (logincallback) activity; this.username = username; this.password = password; }
Comments
Post a Comment