{"id":223,"date":"2018-08-05T07:07:31","date_gmt":"2018-08-05T07:07:31","guid":{"rendered":"https:\/\/ta-laboratories.com\/blog\/?p=223"},"modified":"2018-08-06T07:49:05","modified_gmt":"2018-08-06T07:49:05","slug":"android-app-mysql-connectivity-with-php-and-json","status":"publish","type":"post","link":"https:\/\/ta-laboratories.com\/blog\/2018\/08\/05\/android-app-mysql-connectivity-with-php-and-json\/","title":{"rendered":"Android app MySQL connectivity with PHP and JSON"},"content":{"rendered":"<p><strong>In this blog post we will discuss adding database connectivity to your Android application. We will look at how to make a HTTP POST request from your android application to a server side PHP script, we will also take a look at how to parse the returned JSON encoded results using Volley. <\/strong><\/p>\n<p>\n<strong>The following tools will be used for this tutorial:<\/strong><\/p>\n<li>cPanel<\/li>\n<li>Android Studio<\/li>\n<li>MySQL<\/li>\n<li>PHP<\/>\n<p><\/strong><br \/>\n<strong>Let&#8217;s get started by configuring a new test database in cPanel, find the Databases tab on the menu.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-menu.jpg\" alt=\"\" width=\"742\" height=\"189\" class=\"alignnone size-full wp-image-227\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-menu.jpg 742w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-menu-300x76.jpg 300w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/p>\n<p><strong> Then use the MySQL Database Wizard to configure a new database called &#8220;testdb&#8221;.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-wizard-1024x442.jpg\" alt=\"\" width=\"636\" height=\"275\" class=\"alignnone size-large wp-image-233\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-wizard-1024x442.jpg 1024w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-wizard-300x130.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-wizard-768x332.jpg 768w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-wizard.jpg 1072w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/p>\n<p><strong>Create a user for your new database.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-user.jpg\" alt=\"\" width=\"704\" height=\"410\" class=\"alignnone size-full wp-image-236\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-user.jpg 704w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-user-300x175.jpg 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/p>\n<p><strong>Set user privileges, select &#8220;ALL PRIVILEGES&#8221; to select all.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-user-priv.jpg\" alt=\"\" width=\"1023\" height=\"741\" class=\"alignnone size-full wp-image-237\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-user-priv.jpg 1023w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-user-priv-300x217.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-user-priv-768x556.jpg 768w\" sizes=\"auto, (max-width: 1023px) 100vw, 1023px\" \/><\/p>\n<p><strong>If all went well you should have a database called &#8220;testdb&#8221; now setup on your hosted server. <\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-success-1024x190.jpg\" alt=\"\" width=\"636\" height=\"118\" class=\"alignnone size-large wp-image-238\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-success-1024x190.jpg 1024w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-success-300x56.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-success-768x143.jpg 768w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-success.jpg 1027w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/p>\n<p><strong>Now we will use phpMyAdmin to create a table with some information in our blank database. Let&#8217;s get started by making a new table named &#8220;test_table&#8221; with 2 columns in it, one for an auto increment unique ID and one for some data.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-create-table.jpg\" alt=\"\" width=\"599\" height=\"294\" class=\"alignnone size-full wp-image-240\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-create-table.jpg 599w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-create-table-300x147.jpg 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<p><strong>We will name our first column &#8220;id&#8221; and give it an auto increment INT value for an index. Our second column will be named &#8220;cust_name&#8221; and will be assigned a type of VARCHAR and a length of 45.<\/strong><\/p>\n<p>(Note the picture shows INT for the 2nd data type it should show VARCHAR)<br \/>\n <img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"302\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-table-1024x302.jpg\" alt=\"\" class=\"alignnone size-large wp-image-241\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-table-1024x302.jpg 1024w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-table-300x89.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-table-768x227.jpg 768w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-table.jpg 1322w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><strong>Make sure to set the id field as the primary index key!<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-AI.jpg\" alt=\"\" width=\"458\" height=\"381\" class=\"alignnone size-full wp-image-242\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-AI.jpg 458w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/databases-AI-300x250.jpg 300w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/p>\n<p><strong>Once everything is set up you should have a functional MySQL database, now let&#8217;s write a simple PHP script to interact with it.<\/strong><\/p>\n<p><strong><\/p>\n<div class=\"codecolorer-container php blackboard\" style=\"overflow:auto;white-space:nowrap;width:90%;height:300px;\"><div class=\"php codecolorer\"><span class=\"kw2\">&lt;?php<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co1\">\/\/ Create connection<\/span><br \/>\n<span class=\"re0\">$con<\/span><span class=\"sy0\">=<\/span><a href=\"http:\/\/www.php.net\/mysqli_connect\"><span class=\"kw3\">mysqli_connect<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;localhost&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;admin&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;12345678&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;testdb&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co1\">\/\/ Check connection<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/mysqli_connect_errno\"><span class=\"kw3\">mysqli_connect_errno<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; <span class=\"kw1\">echo<\/span> <span class=\"st0\">&quot;Failed to connect to MySQL: &quot;<\/span> <span class=\"sy0\">.<\/span> <a href=\"http:\/\/www.php.net\/mysqli_connect_error\"><span class=\"kw3\">mysqli_connect_error<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co1\">\/\/ Define input variable(s)<\/span><br \/>\n<span class=\"re0\">$name<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$_REQUEST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;custname&quot;<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"co1\">\/\/$somename = $_REQUEST[&quot;someinput&quot;];<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ This SQL statement selects ALL from the table 'test_table'<\/span><br \/>\n<span class=\"re0\">$sql<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;SELECT * FROM test_table WHERE cust_name = '<span class=\"es4\">$name<\/span>'&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co1\">\/\/ check result<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$result<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/mysqli_query\"><span class=\"kw3\">mysqli_query<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$con<\/span><span class=\"sy0\">,<\/span> <span class=\"re0\">$sql<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$rowcount<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/mysqli_num_rows\"><span class=\"kw3\">mysqli_num_rows<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$result<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ if no results add entry<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$rowcount<\/span> <span class=\"sy0\">==<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$sql<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;INSERT INTO test_table (cust_name)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT * FROM (SELECT '<span class=\"es4\">$name<\/span>') AS tmp<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE NOT EXISTS (SELECT cust_name FROM test_table WHERE cust_name = '<span class=\"es4\">$name<\/span>') LIMIT 1&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> &nbsp; <br \/>\n&nbsp;<br \/>\n<span class=\"co1\">\/\/ Check if there are results<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$result<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/mysqli_query\"><span class=\"kw3\">mysqli_query<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$con<\/span><span class=\"sy0\">,<\/span> <span class=\"re0\">$sql<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span> &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">while<\/span><span class=\"br0\">&#40;<\/span><span class=\"re0\">$row<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$result<\/span><span class=\"sy0\">-&gt;<\/span><span class=\"me1\">fetch_object<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">echo<\/span> <a href=\"http:\/\/www.php.net\/json_encode\"><span class=\"kw3\">json_encode<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$row<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p><\/strong><\/p>\n<p><strong>This PHP script handles database queries on the server end. It also handles authentication with the database and like any other type of server side script, its source code will remain hidden from the end user. We use the customer name from the HTTP request as a variable, the php script will then check the database to see if any rows match its input. If the row count returns 0 a new database entry will be created, if the row count returns 1 then the entry already exists and the values will be returned. Notice we put a limit of one entry per cust_name, this won&#8217;t allow us to add a duplicate record to the table, so for example we could not have 2 of any specific name. <\/p>\n<p>By sending a HTTP request in any browser we should see an echo from the script if things are working right. Remember the first request will not produce a response since the database doesn&#8217;t contain anything yet, so once you send a new name you will have to refresh the page and send the request again to get the results. <\/strong><\/p>\n<p><strong>http:\/\/www.yourdomain.com\/test.php?custname=anyname<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/query.jpg\" alt=\"\" width=\"468\" height=\"89\" class=\"alignnone size-full wp-image-247\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/query.jpg 468w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/query-300x57.jpg 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/p>\n<p><strong>A quick check in phpMyAdmin shows the database entry was created successfully! <\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/db-results.jpg\" alt=\"\" width=\"666\" height=\"358\" class=\"alignnone size-full wp-image-248\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/db-results.jpg 666w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/db-results-300x161.jpg 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/p>\n<p><strong>Now we have to have our app send a database request and parse the results, let&#8217;s start a new Android Studio project.<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty0.jpg\" alt=\"\" width=\"907\" height=\"676\" class=\"alignnone size-full wp-image-250\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty0.jpg 907w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty0-300x224.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty0-768x572.jpg 768w\" sizes=\"auto, (max-width: 907px) 100vw, 907px\" \/><br \/>\n<strong>Select empty activity.<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty.jpg\" alt=\"\" width=\"910\" height=\"680\" class=\"alignnone size-full wp-image-249\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty.jpg 910w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty-300x224.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty-768x574.jpg 768w\" sizes=\"auto, (max-width: 910px) 100vw, 910px\" \/><br \/>\n<strong>Leave the default name of MainActivity<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty2.jpg\" alt=\"\" width=\"908\" height=\"677\" class=\"alignnone size-full wp-image-251\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty2.jpg 908w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty2-300x224.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/AS_empty2-768x573.jpg 768w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/p>\n<p><strong> We will need to add Volley to our app dependency list in the app gradle build file, we can do that by adding this line.<br \/>\n<\/strong><\/p>\n<div class=\"codecolorer-container xml blackboard\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><div class=\"xml codecolorer\">implementation 'com.android.volley:volley:1.0.0'<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/dependencies.jpg\" alt=\"\" width=\"645\" height=\"164\" class=\"alignnone size-full wp-image-252\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/dependencies.jpg 645w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/dependencies-300x76.jpg 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/p>\n<p><strong>We also have to add permission in the Manifest.xml for internet access.<\/strong><\/p>\n<div class=\"codecolorer-container xml blackboard\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><div class=\"xml codecolorer\"><span class=\"re1\">&lt;uses-permission<\/span> <span class=\"re0\">android:name<\/span>=<span class=\"st0\">&quot;android.permission.INTERNET&quot;<\/span> <span class=\"re2\">\/&gt;<\/span><\/div><\/div>\n<div class=\"codecolorer-container xml blackboard\" style=\"overflow:auto;white-space:nowrap;width:90%;height:300px;\"><div class=\"xml codecolorer\"><span class=\"re1\">&lt;?xml<\/span> <span class=\"re0\">version<\/span>=<span class=\"st0\">&quot;1.0&quot;<\/span> <span class=\"re0\">encoding<\/span>=<span class=\"st0\">&quot;utf-8&quot;<\/span><span class=\"re2\">?&gt;<\/span><br \/>\n<span class=\"re1\">&lt;manifest<\/span> <span class=\"re0\">xmlns:android<\/span>=<span class=\"st0\">&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">package<\/span>=<span class=\"st0\">&quot;com.ta_labsllc.mysql&quot;<\/span><span class=\"re2\">&gt;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re1\">&lt;uses-permission<\/span> <span class=\"re0\">android:name<\/span>=<span class=\"st0\">&quot;android.permission.INTERNET&quot;<\/span> <span class=\"re2\">\/&gt;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re1\">&lt;application<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:allowBackup<\/span>=<span class=\"st0\">&quot;true&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:icon<\/span>=<span class=\"st0\">&quot;@mipmap\/ic_launcher&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:label<\/span>=<span class=\"st0\">&quot;@string\/app_name&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:roundIcon<\/span>=<span class=\"st0\">&quot;@mipmap\/ic_launcher_round&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:supportsRtl<\/span>=<span class=\"st0\">&quot;true&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:theme<\/span>=<span class=\"st0\">&quot;@style\/AppTheme&quot;<\/span><span class=\"re2\">&gt;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">&lt;activity<\/span> <span class=\"re0\">android:name<\/span>=<span class=\"st0\">&quot;.MainActivity&quot;<\/span><span class=\"re2\">&gt;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">&lt;intent-filter<span class=\"re2\">&gt;<\/span><\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">&lt;action<\/span> <span class=\"re0\">android:name<\/span>=<span class=\"st0\">&quot;android.intent.action.MAIN&quot;<\/span> <span class=\"re2\">\/&gt;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">&lt;category<\/span> <span class=\"re0\">android:name<\/span>=<span class=\"st0\">&quot;android.intent.category.LAUNCHER&quot;<\/span> <span class=\"re2\">\/&gt;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">&lt;\/intent-filter<span class=\"re2\">&gt;<\/span><\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re1\">&lt;\/activity<span class=\"re2\">&gt;<\/span><\/span><br \/>\n&nbsp; &nbsp; <span class=\"re1\">&lt;\/application<span class=\"re2\">&gt;<\/span><\/span><br \/>\n<br \/>\n<span class=\"re1\">&lt;\/manifest<span class=\"re2\">&gt;<\/span><\/span><\/div><\/div>\n<p><strong>Setup an EditText and InputText fields as well as a Button for the send action in the activity_main.xml layout. <\/strong><\/p>\n<div class=\"codecolorer-container xml blackboard\" style=\"overflow:auto;white-space:nowrap;width:90%;height:300px;\"><div class=\"xml codecolorer\"><span class=\"re1\">&lt;?xml<\/span> <span class=\"re0\">version<\/span>=<span class=\"st0\">&quot;1.0&quot;<\/span> <span class=\"re0\">encoding<\/span>=<span class=\"st0\">&quot;utf-8&quot;<\/span><span class=\"re2\">?&gt;<\/span><br \/>\n<span class=\"re1\">&lt;android.support.constraint.ConstraintLayout<\/span> <span class=\"re0\">xmlns:android<\/span>=<span class=\"st0\">&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">xmlns:app<\/span>=<span class=\"st0\">&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">xmlns:tools<\/span>=<span class=\"st0\">&quot;http:\/\/schemas.android.com\/tools&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">android:layout_width<\/span>=<span class=\"st0\">&quot;match_parent&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">android:layout_height<\/span>=<span class=\"st0\">&quot;match_parent&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">tools:context<\/span>=<span class=\"st0\">&quot;.MainActivity&quot;<\/span><span class=\"re2\">&gt;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re1\">&lt;EditText<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:id<\/span>=<span class=\"st0\">&quot;@+id\/editText&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_width<\/span>=<span class=\"st0\">&quot;wrap_content&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_height<\/span>=<span class=\"st0\">&quot;wrap_content&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginEnd<\/span>=<span class=\"st0\">&quot;84dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginLeft<\/span>=<span class=\"st0\">&quot;85dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginRight<\/span>=<span class=\"st0\">&quot;84dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginStart<\/span>=<span class=\"st0\">&quot;85dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginTop<\/span>=<span class=\"st0\">&quot;16dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:ems<\/span>=<span class=\"st0\">&quot;10&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:gravity<\/span>=<span class=\"st0\">&quot;center_horizontal&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:inputType<\/span>=<span class=\"st0\">&quot;textPersonName&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:text<\/span>=<span class=\"st0\">&quot;@string\/enter_name&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintEnd_toEndOf<\/span>=<span class=\"st0\">&quot;parent&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintStart_toStartOf<\/span>=<span class=\"st0\">&quot;parent&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintTop_toTopOf<\/span>=<span class=\"st0\">&quot;parent&quot;<\/span> <span class=\"re2\">\/&gt;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re1\">&lt;Button<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:id<\/span>=<span class=\"st0\">&quot;@+id\/sendButton&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_width<\/span>=<span class=\"st0\">&quot;wrap_content&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_height<\/span>=<span class=\"st0\">&quot;wrap_content&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginEnd<\/span>=<span class=\"st0\">&quot;148dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginLeft<\/span>=<span class=\"st0\">&quot;148dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginRight<\/span>=<span class=\"st0\">&quot;148dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginStart<\/span>=<span class=\"st0\">&quot;148dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginTop<\/span>=<span class=\"st0\">&quot;16dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:text<\/span>=<span class=\"st0\">&quot;@string\/send&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintEnd_toEndOf<\/span>=<span class=\"st0\">&quot;parent&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintHorizontal_bias<\/span>=<span class=\"st0\">&quot;0.0&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintStart_toStartOf<\/span>=<span class=\"st0\">&quot;parent&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintTop_toBottomOf<\/span>=<span class=\"st0\">&quot;@+id\/resultText&quot;<\/span> <span class=\"re2\">\/&gt;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re1\">&lt;TextView<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:id<\/span>=<span class=\"st0\">&quot;@+id\/resultText&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_width<\/span>=<span class=\"st0\">&quot;185dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_height<\/span>=<span class=\"st0\">&quot;42dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginEnd<\/span>=<span class=\"st0\">&quot;99dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginLeft<\/span>=<span class=\"st0\">&quot;100dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginRight<\/span>=<span class=\"st0\">&quot;99dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginStart<\/span>=<span class=\"st0\">&quot;100dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:layout_marginTop<\/span>=<span class=\"st0\">&quot;16dp&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:gravity<\/span>=<span class=\"st0\">&quot;center_vertical|center_horizontal&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">android:text<\/span>=<span class=\"st0\">&quot;@string\/result&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintEnd_toEndOf<\/span>=<span class=\"st0\">&quot;parent&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintHorizontal_bias<\/span>=<span class=\"st0\">&quot;0.0&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintStart_toStartOf<\/span>=<span class=\"st0\">&quot;parent&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">app:layout_constraintTop_toBottomOf<\/span>=<span class=\"st0\">&quot;@+id\/editText&quot;<\/span> <span class=\"re2\">\/&gt;<\/span><br \/>\n<br \/>\n<span class=\"re1\">&lt;\/android.support.constraint.ConstraintLayout<span class=\"re2\">&gt;<\/span><\/span><\/div><\/div>\n<p><strong>In the main activity we will setup a method called checkDatabase(), we will set up an onClickListener for the send button to call this method anytime the button is pushed. The checkDatabase() method will form a HTTP POST request using params.put() and the string from the inputText field. The request will be sent to the server where the server side php script will process it and make the actual database query. Once finished the php script encodes the results in a JSON format and sends back a reply. When the response listener receives that reply it is parsed using jObject.getString() and the individual row values are accessible.<br \/>\n<strong>MainActivity.java<\/strong><\/p>\n<div class=\"codecolorer-container java blackboard\" style=\"overflow:auto;white-space:nowrap;width:90%;height:300px;\"><div class=\"java codecolorer\"><span class=\"kw1\">package<\/span> <span class=\"co2\">com.ta_labsllc.mysql<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">android.support.v7.app.AppCompatActivity<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">android.os.Bundle<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">android.util.Log<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">android.view.View<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">android.widget.Button<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">android.widget.EditText<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">android.widget.TextView<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.android.volley.Request<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.android.volley.RequestQueue<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.android.volley.Response<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.android.volley.VolleyError<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.android.volley.toolbox.StringRequest<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.android.volley.toolbox.Volley<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">org.json.JSONException<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">org.json.JSONObject<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">java.util.HashMap<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">java.util.Map<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw1\">class<\/span> MainActivity <span class=\"kw1\">extends<\/span> AppCompatActivity <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; EditText inputText<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; TextView outputText<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button\"><span class=\"kw3\">Button<\/span><\/a> sendBtn<span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; @Override<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">protected<\/span> <span class=\"kw4\">void<\/span> onCreate<span class=\"br0\">&#40;<\/span>Bundle savedInstanceState<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">super<\/span>.<span class=\"me1\">onCreate<\/span><span class=\"br0\">&#40;<\/span>savedInstanceState<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; setContentView<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">layout<\/span>.<span class=\"me1\">activity_main<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; inputText <span class=\"sy0\">=<\/span> findViewById<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">editText<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; outputText <span class=\"sy0\">=<\/span> findViewById<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">resultText<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; sendBtn <span class=\"sy0\">=<\/span> findViewById<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">sendButton<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; sendBtn.<span class=\"me1\">setOnClickListener<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">new<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+view\"><span class=\"kw3\">View<\/span><\/a>.<span class=\"me1\">OnClickListener<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> onClick<span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+view\"><span class=\"kw3\">View<\/span><\/a> v<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkDatabase<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> checkDatabase<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; RequestQueue queue <span class=\"sy0\">=<\/span> Volley.<span class=\"me1\">newRequestQueue<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">this<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">final<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a> url <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;http:\/\/yourIPhere.com\/test.php&quot;<\/span><span class=\"sy0\">;<\/span> <span class=\"co1\">\/\/ location of php script<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ send HTTP POST request<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; StringRequest postRequest <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> StringRequest<span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+request\"><span class=\"kw3\">Request<\/span><\/a>.<a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+method\"><span class=\"kw3\">Method<\/span><\/a>.<span class=\"me1\">POST<\/span>, url,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">new<\/span> Response.<span class=\"me1\">Listener<\/span><span class=\"sy0\">&lt;<\/span>String<span class=\"sy0\">&gt;<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> onResponse<span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a> response<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ On response<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span class=\"me1\">d<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;PHP Response&quot;<\/span>, response<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">try<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jObject <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> JSONObject<span class=\"br0\">&#40;<\/span>response<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Pulling items from the array<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a> id <span class=\"sy0\">=<\/span> jObject.<span class=\"me1\">getString<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;id&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <span class=\"co1\">\/\/ parse id <\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a> custName <span class=\"sy0\">=<\/span> jObject.<span class=\"me1\">getString<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;cust_name&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <span class=\"co1\">\/\/ parse name<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span class=\"me1\">d<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;JSON ID&quot;<\/span>, id<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span class=\"me1\">d<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;JSON CUST_NAME&quot;<\/span>, custName<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a> output <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Results: &quot;<\/span> <span class=\"sy0\">+<\/span> id <span class=\"sy0\">+<\/span> <span class=\"st0\">&quot;|&quot;<\/span> <span class=\"sy0\">+<\/span> custName<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputText.<span class=\"me1\">setText<\/span><span class=\"br0\">&#40;<\/span>output<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">catch<\/span> <span class=\"br0\">&#40;<\/span>JSONException e<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ error<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span class=\"me1\">d<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;JSON PARSE&quot;<\/span>, <span class=\"st0\">&quot;ERROR&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">new<\/span> Response.<span class=\"me1\">ErrorListener<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> onErrorResponse<span class=\"br0\">&#40;<\/span>VolleyError error<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ error<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span class=\"me1\">d<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;PHP Error.Response&quot;<\/span>, <span class=\"st0\">&quot;error&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">protected<\/span> Map<span class=\"sy0\">&lt;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a>, String<span class=\"sy0\">&gt;<\/span> getParams<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ encode inputText <\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a> input <span class=\"sy0\">=<\/span> inputText.<span class=\"me1\">getText<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">toString<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span class=\"me1\">d<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;User Input&quot;<\/span>, input<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span class=\"sy0\">&lt;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a>, String<span class=\"sy0\">&gt;<\/span> &nbsp;params <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> HashMap<span class=\"sy0\">&lt;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a>, String<span class=\"sy0\">&gt;<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.<span class=\"me1\">put<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;custname&quot;<\/span>, input<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/params.put(&quot;somerow&quot;, &quot;input&quot;);<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> params<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; queue.<span class=\"me1\">add<\/span><span class=\"br0\">&#40;<\/span>postRequest<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/finished.jpg\" alt=\"\" width=\"845\" height=\"406\" class=\"alignnone size-full wp-image-255\" srcset=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/finished.jpg 845w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/finished-300x144.jpg 300w, https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/finished-768x369.jpg 768w\" sizes=\"auto, (max-width: 845px) 100vw, 845px\" \/><\/p>\n<p><strong>When everything is up and working you should be able to send a name to the database, if the entry exists already it will be returned with its unique id, if it is not it will be added and returned on the next request.<\/strong><\/p>\n<p><strong>Thanks for checking out this tutorial on using MySQL with Android!<\/strong><\/p>\n<div style=\"width: 400px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-223-1\" width=\"400\" height=\"640\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/device-2018-08-05-045724.mp4?_=1\" \/><a href=\"https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/device-2018-08-05-045724.mp4\">https:\/\/ta-laboratories.com\/blog\/wp-content\/uploads\/2018\/08\/device-2018-08-05-045724.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post we will discuss adding database connectivity to your Android application. We will look at how to make a HTTP POST request from your android application to a server side PHP script, we will also take a&#8230; <a href=\"https:\/\/ta-laboratories.com\/blog\/2018\/08\/05\/android-app-mysql-connectivity-with-php-and-json\/\" class=\"read-more\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":257,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12,13,5],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-mysql","category-php","category-programming"],"_links":{"self":[{"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":57,"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":308,"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions\/308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/media\/257"}],"wp:attachment":[{"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ta-laboratories.com\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}