java - Running android studio app on physical device "ERROR IN NETWORK CONNECTION" -
whenever try login, register, change password or reset password app says "error in network connection".
i using strong internet connection, please advise have tried on wi-fi , 4g, still says same error. using wamp server connect phpmyadmin database. please , advise.
login. java file
public class login extends activity { button btnlogin; button btnregister; button passreset; edittext inputemail; edittext inputpassword; private textview loginerrormsg; /** * called when activity first created. */ private static string key_success = "success"; private static string key_uid = "uid"; private static string key_username = "uname"; private static string key_firstname = "fname"; private static string key_lastname = "lname"; private static string key_email = "email"; private static string key_created_at = "created_at"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); inputemail = (edittext) findviewbyid(r.id.email); inputpassword = (edittext) findviewbyid(r.id.pword); btnregister = (button) findviewbyid(r.id.registerbtn); btnlogin = (button) findviewbyid(r.id.loginbtn); passreset = (button) findviewbyid(r.id.passres); loginerrormsg = (textview) findviewbyid(r.id.loginerrormsg); passreset.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent myintent = new intent(view.getcontext(), passwordreset.class); startactivityforresult(myintent, 0); finish(); }}); btnregister.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent myintent = new intent(view.getcontext(), register.class); startactivityforresult(myintent, 0); finish(); }}); /** * login button click event * toast set alert when email , password field empty **/ btnlogin.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { if ((!inputemail.gettext().tostring().equals("")) && (!inputpassword.gettext().tostring().equals(""))) { netasync(view); } else if ((!inputemail.gettext().tostring().equals(""))) { toast.maketext(getapplicationcontext(), "password field empty", toast.length_short).show(); } else if ((!inputpassword.gettext().tostring().equals(""))) { toast.maketext(getapplicationcontext(), "email field empty", toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "email , password field empty", toast.length_short).show(); } } }); } /** * async task check whether internet connection working. **/ private class netcheck extends asynctask<string,string,boolean> { private progressdialog ndialog; @override protected void onpreexecute(){ super.onpreexecute(); ndialog = new progressdialog(login.this); ndialog.settitle("checking network"); ndialog.setmessage("loading.."); ndialog.setindeterminate(false); ndialog.setcancelable(true); ndialog.show(); } /** * gets current device state , checks working internet connection trying google. **/ @override protected boolean doinbackground(string... args){ connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnected()) { try { url url = new url("http://www.google.com"); httpurlconnection urlc = (httpurlconnection) url.openconnection(); urlc.setconnecttimeout(3000); urlc.connect(); if (urlc.getresponsecode() == 200) { return true; } } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return false; } @override protected void onpostexecute(boolean th){ if(th == true){ ndialog.dismiss(); new processlogin().execute(); } else{ ndialog.dismiss(); loginerrormsg.settext("error in network connection"); } } } /** * async task , send data sql database through json respone. **/ private class processlogin extends asynctask<string, string, jsonobject> { private progressdialog pdialog; string email,password; @override protected void onpreexecute() { super.onpreexecute(); inputemail = (edittext) findviewbyid(r.id.email); inputpassword = (edittext) findviewbyid(r.id.pword); email = inputemail.gettext().tostring(); password = inputpassword.gettext().tostring(); pdialog = new progressdialog(login.this); pdialog.settitle("contacting servers"); pdialog.setmessage("logging in ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected jsonobject doinbackground(string... args) { userfunctions userfunction = new userfunctions(); jsonobject json = userfunction.loginuser(email, password); return json; } @override protected void onpostexecute(jsonobject json) { try { if (json.getstring(key_success) != null) { string res = json.getstring(key_success); if(integer.parseint(res) == 1){ pdialog.setmessage("loading user space"); pdialog.settitle("getting data"); databasehandler db = new databasehandler(getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); /** * clear previous data in sqlite database. **/ userfunctions logout = new userfunctions(); logout.logoutuser(getapplicationcontext()); db.adduser(json_user.getstring(key_firstname),json_user.getstring(key_lastname),json_user.getstring(key_email),json_user.getstring(key_username),json_user.getstring(key_uid),json_user.getstring(key_created_at)); /** *if json array details stored in sqlite launches user panel. **/ intent upanel = new intent(getapplicationcontext(), main.class); upanel.addflags(intent.flag_activity_clear_top); pdialog.dismiss(); startactivity(upanel); /** * close login screen **/ finish(); }else{ pdialog.dismiss(); loginerrormsg.settext("incorrect username/password"); } } } catch (jsonexception e) { e.printstacktrace(); } } } public void netasync(view view){ new netcheck().execute(); } }
register.java file
public class register extends activity { /** * json response node names. **/ private static string key_success = "success"; private static string key_uid = "uid"; private static string key_firstname = "fname"; private static string key_lastname = "lname"; private static string key_username = "uname"; private static string key_email = "email"; private static string key_created_at = "created_at"; private static string key_error = "error"; /** * defining layout items. **/ edittext inputfirstname; edittext inputlastname; edittext inputusername; edittext inputemail; edittext inputpassword; button btnregister; textview registererrormsg; /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.register); /** * defining layout items **/ inputfirstname = (edittext) findviewbyid(r.id.fname); inputlastname = (edittext) findviewbyid(r.id.lname); inputusername = (edittext) findviewbyid(r.id.uname); inputemail = (edittext) findviewbyid(r.id.email); inputpassword = (edittext) findviewbyid(r.id.pword); btnregister = (button) findviewbyid(r.id.register); registererrormsg = (textview) findviewbyid(r.id.register_error); /** * button switches login screen on clicked **/ button login = (button) findviewbyid(r.id.bktologin); login.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent myintent = new intent(view.getcontext(), login.class); startactivityforresult(myintent, 0); finish(); } }); /** * register button click event. * toast set alert when fields empty. * toast set alert username must 5 characters. **/ btnregister.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if ( ( !inputusername.gettext().tostring().equals("")) && ( !inputpassword.gettext().tostring().equals("")) && ( !inputfirstname.gettext().tostring().equals("")) && ( !inputlastname.gettext().tostring().equals("")) && ( !inputemail.gettext().tostring().equals("")) ) { if ( inputusername.gettext().tostring().length() > 4 ){ netasync(view); } else { toast.maketext(getapplicationcontext(), "username should minimum 5 characters", toast.length_short).show(); } } else { toast.maketext(getapplicationcontext(), "one or more fields empty", toast.length_short).show(); } } }); } /** * async task check whether internet connection working **/ private class netcheck extends asynctask<string,string,boolean> { private progressdialog ndialog; @override protected void onpreexecute(){ super.onpreexecute(); ndialog = new progressdialog(register.this); ndialog.setmessage("loading.."); ndialog.settitle("checking network"); ndialog.setindeterminate(false); ndialog.setcancelable(true); ndialog.show(); } @override protected boolean doinbackground(string... args){ /** * gets current device state , checks working internet connection trying google. **/ connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnected()) { try { url url = new url("http://www.google.com"); httpurlconnection urlc = (httpurlconnection) url.openconnection(); urlc.setconnecttimeout(3000); urlc.connect(); if (urlc.getresponsecode() == 200) { return true; } } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return false; } @override protected void onpostexecute(boolean th){ if(th == true){ ndialog.dismiss(); new processregister().execute(); } else{ ndialog.dismiss(); registererrormsg.settext("error in network connection"); } } } private class processregister extends asynctask<string, string, jsonobject> { /** * defining process dialog **/ private progressdialog pdialog; string email,password,fname,lname,uname; @override protected void onpreexecute() { super.onpreexecute(); inputusername = (edittext) findviewbyid(r.id.uname); inputpassword = (edittext) findviewbyid(r.id.pword); fname = inputfirstname.gettext().tostring(); lname = inputlastname.gettext().tostring(); email = inputemail.gettext().tostring(); uname= inputusername.gettext().tostring(); password = inputpassword.gettext().tostring(); pdialog = new progressdialog(register.this); pdialog.settitle("contacting servers"); pdialog.setmessage("registering ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected jsonobject doinbackground(string... args) { userfunctions userfunction = new userfunctions(); jsonobject json = userfunction.registeruser(fname, lname, email, uname, password); return json; } @override protected void onpostexecute(jsonobject json) { /** * checks success message. **/ try { if (json.getstring(key_success) != null) { registererrormsg.settext(""); string res = json.getstring(key_success); string red = json.getstring(key_error); if(integer.parseint(res) == 1){ pdialog.settitle("getting data"); pdialog.setmessage("loading info"); registererrormsg.settext("successfully registered"); databasehandler db = new databasehandler(getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); /** * removes previous data in sqlite database **/ userfunctions logout = new userfunctions(); logout.logoutuser(getapplicationcontext()); db.adduser(json_user.getstring(key_firstname),json_user.getstring(key_lastname),json_user.getstring(key_email),json_user.getstring(key_username),json_user.getstring(key_uid),json_user.getstring(key_created_at)); /** * stores registered data in sqlite database * launch registered screen **/ intent registered = new intent(getapplicationcontext(), registered.class); /** * close views before launching registered screen **/ registered.addflags(intent.flag_activity_clear_top); pdialog.dismiss(); startactivity(registered); finish(); } else if (integer.parseint(red) ==2){ pdialog.dismiss(); registererrormsg.settext("user exists"); } else if (integer.parseint(red) ==3){ pdialog.dismiss(); registererrormsg.settext("invalid email id"); } } else{ pdialog.dismiss(); registererrormsg.settext("error occured in registration"); } } catch (jsonexception e) { e.printstacktrace(); } }} public void netasync(view view){ new netcheck().execute(); }}
manifest.java file
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.brad.visor" > <application android:icon="@mipmap/logo" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".login"> </activity> <activity android:name=".register"> </activity> <activity android:name=".registered"> </activity> <activity android:name=".main"> </activity> <activity android:name=".passwordreset"> </activity> <activity android:name=".changepassword"> </activity> </application> <!-- allow connect internet , know current network state--> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.access_network_state" /> </manifest>
i think problem somewhere in doinbackground method.
networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnected()) { try { url url = new url("http://www.google.com"); httpurlconnection urlc = (httpurlconnection) url.openconnection(); urlc.setconnecttimeout(3000); urlc.connect(); log.i("code",urlc.getresponsecode() ); if (urlc.getresponsecode() == 200) { return true; } } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
Comments
Post a Comment