php - Connecting database to android using json -
as new application development using json connect database using php having error following code:
public class mainactivity extends activity { edittext etemail, etpass; button login, register; textview terror; progressdialog pdialog; //textview ptodoc; private static string key_success = "success"; //private static string key_error = "error"; private static final string key_referred_as = "referred_as"; private static final string key_email = "email"; private static final string key_card = "card"; private static final string key_address = "address"; private static final string key_contact_no = "contact_no"; private static final string key_id = "id"; private static final string key_dateofbirth = "dateofbirth"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setting default screen sign_in.xml setcontentview(r.layout.activity_main); etemail = (edittext) findviewbyid(r.id.emailaddress); etpass = (edittext) findviewbyid(r.id.password); terror = (textview) findviewbyid(r.id.error); login = (button) findviewbyid(r.id.login); register = (button) findviewbyid(r.id.reg); login.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view){ string email = etemail.gettext().tostring(); string password = etpass.gettext().tostring(); userfunctions userfunction = new userfunctions(); log.d("button", "login"); jsonobject json = userfunction.loginuser(email, password); try { if (json.getstring(key_success) == null) { terror.settext(""); string res = json.getstring(key_success); if(integer.parseint(res) == 1){ // user logged in // store user details in sqlite database databasehandler db = new databasehandler(getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); // clear previous data in database userfunction.logoutuser(getapplicationcontext()); db.adduser(json_user.getstring(key_referred_as),json_user.getstring(key_email),json_user.getstring(key_card), json_user.getstring(key_dateofbirth),json_user.getstring(key_address), json_user.getstring(key_contact_no),json_user.getstring(key_id)); // launch dashboard screen intent dashboard = new intent(getapplicationcontext(), home.class); // close views before launching dashboard dashboard.addflags(intent.flag_activity_clear_top); startactivity(dashboard); // close login screen finish(); }else{ // error in login terror.settext("incorrect username/password"); } } } catch (jsonexception e) { e.printstacktrace(); } } }); register.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent = new intent(getapplicationcontext(),register.class); startactivity(i); } }); } }
on click event of login button email , password , add jsonobject , pass asyn class send particular weblink related code follows:
class sendasyn extends asynctask<jsonobject, jsonobject, jsonobject> { string url = "http://yourweblink.com/yourdbfile.php"; boolean repeat=true; @override protected jsonobject doinbackground(jsonobject... params) { // todo auto-generated method stub jsonobject json = params[0]; httpclient client = new defaulthttpclient(); httpconnectionparams.setconnectiontimeout(client.getparams(), 100000); string resfromserver="null"; jsonobject jsonresponse = null; httppost post = new httppost(url); try { stringentity se = new stringentity("regis=" + json.tostring()); post.addheader("content-type", "application/x-www-form-urlencoded"); post.setentity(se); httpresponse response; log.i("url",post.getparams().tostring()); response = client.execute(post); resfromserver = org.apache.http.util.entityutils .tostring(response.getentity()); if(resfromserver.trim().equals("successfull")){ loginactivity.suc=true; loginactivity.error=false; log.i("here are","successfull"+resfromserver); }else{ string[] tokens=resfromserver.split("~"); log.i("here else",resfromserver); if(tokens[0].equals("error")){ log.i("here error",tokens[1]); loginactivity.error=true; loginactivity.suc=false; } } //log.i("response", resfromserver); } catch (exception e) { e.printstacktrace(); } return jsonresponse; }
}
and call class using
sendasyn transmitter = new sendasyn(); transmitter.execute(new jsonobject[]{your jsonobject});
Comments
Post a Comment