Monday, 15 September 2014

java - Login form using post only works once? cant find the error -



java - Login form using post only works once? cant find the error -

i have had problem , cant figure out how sort out. using android studio compile project. problem im having can send login info 1 time success weird me. not advanced android programmer.

here code im having problem with:

public void postdata(string valueiwanttosend) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(login_url); seek { list namevaluepairs = new arraylist(); namevaluepairs.add(new basicnamevaluepair("username", user.gettext().tostring())); namevaluepairs.add(new basicnamevaluepair("password", pass.gettext().tostring())); httppost.setentity(new urlencodedformentity(namevaluepairs)); response = httpclient.execute(httppost); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } } private class myasynctask extends asynctask<string, integer, double>{ jsonarray jsonarray = null; httpentity entity = null; string responsestr = null; jsonobject json = null; @override protected double doinbackground(string... params) { postdata(params[0]); homecoming null; } protected void onpostexecute(double result){ pb.setvisibility(view.gone); entity = response.getentity(); seek { responsestr = entityutils.tostring(entity); toast.maketext(getapplicationcontext(), "we have response string", toast.length_short) .show(); jsonarray = new jsonarray(responsestr); } grab (jsonexception e){ e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } integer success = 0; (int i=0; i<jsonarray.length(); i++) { json = null; seek { json = jsonarray.getjsonobject(i); success = json.getint("success"); toast.maketext(getapplicationcontext(), "we have return", toast.length_short) .show(); } grab (jsonexception e) { e.printstacktrace(); } } if (success == 1){ toast.maketext(getapplicationcontext(), "login successful", toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "login failed", toast.length_short).show(); } } protected void onprogressupdate(integer... progress){ pb.setprogress(progress[0]); } }

now said works 1 time becomes non responsive if retry loging in or correcting username password?

really not sure how prepare this. if have left out info allow me know , happy provide you.

edit 1(could long night):

ok have seen post works each time before application goes non responsive. seems if retrieves info 1 time , never 1 time again if application closed , reopened. work 1 time again if recompile android studio goes non responsive sate :(.

edit 2:

sorry guys here how phone call async task onclick event attached login button

public void sendmessage(view view){ string s1, s2; s1 = user.gettext().tostring(); s2 = pass.gettext().tostring(); if ((s1.matches("")) || (s2.matches(""))) { toast.maketext(this, "please fill out fields.", toast.length_short).show(); } else { //intent intent = new intent(this, com.example.studentpa.dashboard.class); //startactivity(intent); pb.setvisibility(view.visible); new myasynctask().execute(s1); } }

i have found solution problem, please see comments changes made, here code works.

public void loginmethod(view view) { em = email.gettext().tostring(); ps = pass.gettext().tostring(); if (isconnected()) { if (em.contains(" ") || ps.contains(" ")) { toast.maketext(getapplicationcontext(), "no spaces allowed in either field", toast.length_short).show(); } else if (em.matches("") || ps.matches("")) { toast.maketext(getapplicationcontext(), "please fill in fields", toast.length_short).show(); } else { pb.setvisibility(view.visible); new myasynctask().execute(em); email.setenabled(false); pass.setenabled(false); } } else { toast.maketext(getapplicationcontext(), "you not connected internet", toast.length_short).show(); } } private class myasynctask extends asynctask<string, integer, double> { @override protected double doinbackground(string... params) { postdata(params[0]); homecoming null; } protected void onpostexecute(double result) { if (strresponse.isempty()){ toast.maketext(getapplicationcontext(), "we have no response server", toast.length_long).show(); } else { seek { jsonarray = new jsonarray(strresponse); } grab (jsonexception e){ e.printstacktrace(); } jsonobject json; json = null; integer success = 0; (int i=0; i<jsonarray.length(); i++) { json = null; seek { json = jsonarray.getjsonobject(i); success = json.getint("success"); } grab (jsonexception e) { e.printstacktrace(); } } if (success == 1){ toast.maketext(getapplicationcontext(), "login successful", toast.length_short).show(); intent intent = new intent(getapplicationcontext(), dashboard.class); startactivity(intent); } else if (success == 0){ toast.maketext(getapplicationcontext(), "incorrect login details", toast.length_short).show(); } } email.setenabled(true); pass.setenabled(true); pb.setvisibility(view.gone); } protected void onprogressupdate(integer... progress) { pb.setprogress(progress[0]); } public void postdata(string s1) { // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.gbes.co.za"); httpentity entity; seek { // add together info list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("username", em)); namevaluepairs.add(new basicnamevaluepair("password", ps)); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); entity = response.getentity(); strresponse = entityutils.tostring(entity); } grab (httpresponseexception e) { e.printstacktrace(); } grab (httphostconnectexception e){ e.printstacktrace(); } grab (clientprotocolexception e){ e.printstacktrace(); } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } } }

edit:

hey guys here server code: database connection hidden server protection

<?php $uname=$_post['username']; $pword=$_post['password']; $squl = "select * `usr` `ue`='".$uname."' && `up`='".$pword."' limit 1"; $query = mysql_query($squl); if (mysql_num_rows($query) == 1) { date_default_timezone_set('africa/johannesburg'); while($rows = mysql_fetch_array($query)){ $logincount=$rows['ulc']; $logincount=$logincount + 1; $uid=$rows['id']; } $nowtime = date("y-m-d h:i:s"); $loginsql = "update `users` set `ulc`=$logincount, `ull`='$nowtime'"; $query = mysql_query($loginsql); $login_ok = true; } else { $login_ok = false; } if ($login_ok) { $response["success"] = 1; $response["message"] = "login successful!"; die("[".json_encode($response)."]"); } else { $response["success"] = 0; $response["message"] = "invalid credentials!"; die("[".json_encode($response)."]"); } mysql_close($db); header('content-type: text/html; charset=utf-8'); ?>

java android android-activity httprequest

No comments:

Post a Comment