php - Can't connect Android application to MySQL database using WAMP as local server -
i'm trying create simple login android application compares input user data database following these tutorials:
- http://androidcss.com/android/test-android-app-php-localhost-wamp/
- http://androidcss.com/android/android-php-mysql-login-tutorial/
i'm using wamp local server test if can make connection. have 2 php-files called config.inc.php , login.inc.php placed in following directory: c:\wamp64\www\duft , this:
config.inc.php
<?php $servername = "localhost:80"; $username = "root"; $password = "root"; $dbname = "duft"; try { $conn = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch(pdoexception $e) { die("oops went wrong"); } ?>
login.inc.php
<?php include 'config.inc.php'; // check whether username or password set android if(isset($_post['username']) && isset($_post['password'])) { // innitialize variable $result=''; $username = $_post['username']; $password = $_post['password']; // query database row exist or not $sql = 'select * tbl_login email = :username , adgangskode = :password'; $stmt = $conn->prepare($sql); $stmt->bindparam(':username', $username, pdo::param_str); $stmt->bindparam(':password', $password, pdo::param_str); $stmt->execute(); if($stmt->rowcount()) { $result="true"; } elseif(!$stmt->rowcount()) { $result="false"; } // send result android echo $result; }?>
then have loginactivity class i'm using asynctask make connection database in background. in onpostexecute() method i'm trying start new activity if user input matches in database. keep getting error php-files:
( ! ) warning: pdo::__construct(): mysql server has gone away in c:\wamp64\www\duft\login.inc.php on line 10call stack#timememoryfunctionlocation10.0042244400{main}( )...\login.inc.php:020.0043245592http://www.php.net/pdo.construct' target='_new'>__construct( )...\login.inc.php:10( ! ) warning: pdo::__construct(): error while reading greeting packet. pid=9636 in c:\wamp64\www\duft\login.inc.php on line 10call stack#timememoryfunctionlocation10.0042244400{main}( )...\login.inc.php:020.0043245592http://www.php.net/pdo.construct' target='_new'>__construct( )...\login.inc.php:10oops went wrong
this logcat says:
my loginactivity java class looks this:
public class loginactivity extends appcompatactivity{ //nyt // connection_timeout , read_timeout in milliseconds public static final int connection_timeout=2000000000; public static final int read_timeout=2000000000; private edittext etemail; private edittext etpassword; //nyt protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); //nyt // reference variables etemail = (edittext) findviewbyid(r.id.email); etpassword = (edittext) findviewbyid(r.id.password); //nyt textview klikher = (textview) findviewbyid(r.id.klikher); klikher.setpaintflags(klikher.getpaintflags() | paint.underline_text_flag); button login = (button) findviewbyid(r.id.signin); login.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { //intent intent = new intent(loginactivity.this, menuscreen.class); //startactivity(intent); //nyt // text email , passord field final string email = etemail.gettext().tostring(); final string password = etpassword.gettext().tostring(); // initialize asynclogin() class email , password new asynclogin().execute(email, password); //nyt } }); } private class asynclogin extends asynctask<string, string, string> { progressdialog pdloading = new progressdialog(loginactivity.this); httpurlconnection conn; url url = null; @override protected void onpreexecute() { super.onpreexecute(); //this method running on ui thread pdloading.setmessage("\tloading..."); pdloading.setcancelable(false); pdloading.show(); } @override protected string doinbackground(string... params) { try { // enter url address php file resides url = new url("http://192.168.87.100/duft/login.inc.php"); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); return "exception"; } try { // setup httpurlconnection class send , receive data php , mysql conn = (httpurlconnection) url.openconnection(); //conn.setreadtimeout(read_timeout); //conn.setconnecttimeout(connection_timeout); conn.setrequestmethod("post"); // setdoinput , setdooutput method depict handling of both send , receive conn.setdoinput(true); conn.setdooutput(true); // append parameters url uri.builder builder = new uri.builder() .appendqueryparameter("username", params[0]) .appendqueryparameter("password", params[1]); string query = builder.build().getencodedquery(); // open connection sending data outputstream os = conn.getoutputstream(); bufferedwriter writer = new bufferedwriter( new outputstreamwriter(os, "utf-8")); writer.write(query); writer.flush(); writer.close(); os.close(); conn.connect(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); return "exception"; } try { int response_code = conn.getresponsecode(); // check if successful connection made if (response_code == httpurlconnection.http_ok) { // read data sent server inputstream input = conn.getinputstream(); bufferedreader reader = new bufferedreader(new inputstreamreader(input)); stringbuilder result = new stringbuilder(); string line; while ((line = reader.readline()) != null) { result.append(line); } // pass data onpostexecute method return (result.tostring()); } else { return ("unsuccessful"); } } catch (ioexception e) { e.printstacktrace(); return "exception"; } { conn.disconnect(); } } @override protected void onpostexecute(string result) { //this method running on ui thread pdloading.dismiss(); if (result.equalsignorecase("true")) { /* here launching activity when login successful. if persist login state use sharedpreferences of android. , logout button clear sharedpreferences. */ intent intent = new intent(loginactivity.this, menuscreen.class); startactivity(intent); loginactivity.this.finish(); } else if (result.equalsignorecase("false")) { // if username , password not match display error message //toast.maketext(loginactivity.this, "invalid email or password", toast.length_long).show(); } else if (result.equalsignorecase("exception") || result.equalsignorecase("unsuccessful")) { //toast.maketext(loginactivity.this, "oops! went wrong. connection problem.", toast.length_long).show(); } } }}
i have followed steps in tutorials, , i'm able access local server phone entering ipv4 address mobile browser. must mean i'm able access database on local server, right?
i found solution! problem in line of code
php $servername = "localhost:80";
here define port apache instead of mysql 3307. changed right port 3307 :)
thanks help!
Comments
Post a Comment