java - Unable to send data to server in Android -
i'm new android. don't know part has gone wrong. thing i'm unable send data server in android studio.
this error i'm facing
fatal error: uncaught error: call undefined function mysql_connect() in c:\xampp\htdocs\students\connection.php:6 stack trace: #0 c:\xampp\htdocs\students\add_employee.php(2): include() #1 {main} thrown in c:\xampp\htdocs\students\connection.php on line 6
the code goes this...
main activity
public class mainactivity extends appcompatactivity { button b1; edittext e1; private progressdialog pdialog; private jsonobject json; private int success=0; private httpurlconnection service; private string strname =""; //initialize webservice url private string path = "http://localhost/student/add_employee.php"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); b1 = (button) findviewbyid(r.id.button); e1 = (edittext) findviewbyid(r.id.edittext9); service=new httpurlconnection(); b1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (!e1.gettext().tostring().equals("") ) { strname = e1.gettext().tostring(); //call webservice new postdatatoserver().execute(); } else { toast.maketext(getapplicationcontext(), "please enter fields", toast.length_long).show(); } intent intent = new intent(mainactivity.this, student1.class); startactivity(intent); } }); } private class postdatatoserver extends asynctask<void, void, void> { string response = ""; //create hashmap object send parameters web service hashmap<string, string> postdataparams; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("please wait..."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { postdataparams=new hashmap<string, string>(); postdataparams.put("name", strname); //call serverdata() method call webservice , store result in response response= service.serverdata(path,postdataparams); try { json = new jsonobject(response); //get values jsonobject system.out.println("success=" + json.get("success")); success = json.getint("success"); } catch (jsonexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); if (pdialog.isshowing()) pdialog.dismiss(); if(success==1) { toast.maketext(getapplicationcontext(), "employee added successfully..!", toast.length_long).show(); } } } }
httpurlconnection
public class httpurlconnection { string response=""; url url; public string serverdata(string path,hashmap<string, string> params) { try { url = new url(path); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(15000); conn.setconnecttimeout(15000); conn.setrequestmethod("post"); conn.setdoinput(true); conn.setdooutput(true); outputstream os = conn.getoutputstream(); bufferedwriter writer = new bufferedwriter( new outputstreamwriter(os, "utf-8")); writer.write(getpostdatastring(params)); writer.flush(); writer.close(); os.close(); int responsecode = conn.getresponsecode(); if (responsecode == httpsurlconnection.http_ok) { string line; bufferedreader br = new bufferedreader(new inputstreamreader(conn.getinputstream())); //log.d("output",br.tostring()); while ((line = br.readline()) != null) { response += line; log.d("output lines", line); } } else { response = ""; } } catch (exception e) { e.printstacktrace(); } return response; } private string getpostdatastring(hashmap<string, string> params) throws unsupportedencodingexception { stringbuilder result = new stringbuilder(); boolean first = true; for(map.entry<string, string> entry : params.entryset()){ if (first) first = false; else result.append("&"); result.append(urlencoder.encode(entry.getkey(), "utf-8")); result.append("="); result.append(urlencoder.encode(entry.getvalue(), "utf-8")); } return result.tostring(); }}
my php code
add_employee.php
<?php include('connection.php'); $emp_name=$_post["name"]; $success=0; $status="active"; $sql = "insert `employee` (`emp_name`) values ('$emp_name')"; if(mysql_query($sql)) { $success=1; } $response["success"]=$success; die(json_encode($response)); mysql_close($con); ?>
connection.php
<?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(!$conn) { die('could not connect: ' . mysql_error()); } mysql_select_db('student'); ?>
Comments
Post a Comment