php - Application does not do anything when inserting values to MYSQL using Android -
i trying develop , android application can insert values in mysql database using android application. not able values stored in database. have followed few tutorials , tried till have had no luck.
could 1 let me know code have used correct.
the have used below code insert values values mysql database
package com.example.androidsql.mysqlsample01activity; import android.os.asynctask; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.apache.http.util.entityutils; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.arraylist; import java.util.list; public class mysqlsample01activity extends actionbaractivity { private edittext edittextname; private edittext edittextadd; private edittext editdepartment; private static final string tag = "mysqlsample01activity"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_mysql_sample01); edittextname = (edittext) findviewbyid(r.id.edittextname); edittextadd = (edittext) findviewbyid(r.id.edittextaddress); editdepartment = (edittext) findviewbyid(r.id.department); } public void insert(view view){ string name = edittextname.gettext().tostring(); string add = edittextadd.gettext().tostring(); string dept = editdepartment.gettext().tostring(); inserttodatabase(name,add,dept); } private void inserttodatabase(string name, string add,string dept){ class sendpostreqasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { try { posttext (); }catch (nullpointerexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(string result) { super.onpostexecute(result); toast.maketext(getapplicationcontext(),"result", toast.length_long).show(); textview textviewresult = (textview) findviewbyid(r.id.textviewresult); textviewresult.settext("inserted"); } } sendpostreqasynctask sendpostreqasynctask = new sendpostreqasynctask(); sendpostreqasynctask.execute(name, add); } @suppresswarnings("deprecation") private void posttext () { try { string name = edittextname.gettext().tostring(); string add = edittextadd.gettext().tostring(); string dept = editdepartment.gettext().tostring(); // adding data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("name",name)); namevaluepairs.add(new basicnamevaluepair("address",add)); namevaluepairs.add(new basicnamevaluepair("depart",dept)); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://192.168.2.5/nfcams/insertingcustodian.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); //is = entity.getcontent(); if (entity != null) { string responsestr = entityutils.tostring(entity).trim(); log.v(tag, "response: " + responsestr); // can add if statement here , other actions based on response } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_mysql_sample01, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
the manifest code below.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidsql.mysqlsample01activity" > <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mysqlsample01activity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
the layout code stated below.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mysqlsample01activity"> <textview android:id="@+id/textviewresult" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittextname" android:layout_below="@+id/textviewresult" android:layout_margintop="71dp" android:inputtype="text" android:layout_alignparentend="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittextaddress" android:layout_centervertical="true" android:layout_alignparentstart="true" android:inputtype="text" android:layout_alignend="@+id/edittextname" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new button" android:id="@+id/button" android:onclick="insert" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/department" android:inputtype="text" android:layout_below="@+id/edittextaddress" android:layout_alignparentstart="true" android:layout_margintop="50dp" android:layout_alignparentend="true" /> </relativelayout>
the php code have used
<?php include("connection.php"); $name = $_post['name']; $design = $_post['address']; $depart = $_post['depart']; //$sqlinsert = "insert `custodian` (`name`, `design`, `depart`) values ('$name', '$design', '$depart')"; $sqlinsert = "insert store_users(`custname`, `custdesign`,`custdepart`) values ('$custname', '$custdesign','$custdepart')"; $response = array(); if(mysqli_query($con,$sqlinsert)) { // inserted database $response["success"] = 1; $response["message"] = "product created."; // echoing json response echo json_encode($response); } else { // failed insert row $response["success"] = 0; $response["message"] = "oops! error occurred."; // echoing json response echo json_encode($response); } mysqli_close($con); ?>
first of all, suggest use library networking ion (https://github.com/koush/ion) or retrofit (http://square.github.io/retrofit/) depends on more suitable you. these libs make more easy networking without pain. example, trying do, ion this:
public void insert(view view) { string name = edittextname.gettext().tostring(); string add = edittextadd.gettext().tostring(); string dept = editdepartment.gettext().tostring(); ion.with(this) .load("http://192.168.2.5/nfcams/insertingcustodian.php") .setbodyparameter("name", name) .setbodyparameter("address", add) .setbodyparameter("depart", dept) .asstring() .setcallback(new futurecallback<string>() { @override public void oncompleted(exception e, string result) { log.v(tag, "response: " + result); } }); }
tell me if helped or not. can post how should using previous method, don't see errors there on first look.
Comments
Post a Comment