On Android we have a lot of database to use, which is SQLite. But if we want to use MySQL or any other database, then install it on our own server and use it. Those web developers, almost all of them are familiar with MySQL MySQL has many web apps to access MySQL from various apps we create. We will look at a small tutorial today, how to save data from the Android phone to a database of data servers. And as a server side programming language we will use PHP. If you do not have any server, then your local PHP and MySQL can be installed. It is easy to install and install it manually using WAMP server. If there is no idea about these, the following can be seen.
We’ll make a very simple app. From where only the user name and email server can be saved. For this we will create a database. And create a table in it, which will consist of two columns. With phpMyAdmin, you can easily do this with a graphical user interface. Below we give a screen shot for creating database columns
Send data from our Android apps to the server, receive the data and save it to the database, php code is needed. The PHP code for this app, assuming the name of the file is process.php
From the app to the data server / HTTP Post, our app needs internet access. For this, add the permissions below at Andorid Mainifests
Facebook
RSS
CYBER SECURITY
How to send Data To Database From Android + PHP + MySQL Tutorial.
On Android we have a lot of database to use, which is SQLite. But if we want to use MySQL or any other database, then install it on our own server and use it. Those web developers, almost all of them are familiar with MySQL MySQL has many web apps to access MySQL from various apps we create. We will look at a small tutorial today, how to save data from the Android phone to a database of data servers. And as a server side programming language we will use PHP. If you do not have any server, then your local PHP and MySQL can be installed. It is easy to install and install it manually using WAMP server. If there is no idea about these, the following can be seen .
We’ll make a very simple app. From where only the user name and email server can be saved. For this we will create a database. And create a table in it, which will consist of two columns. With phpMyAdmin, you can easily do this with a graphical user interface. Below we give a screen shot for creating database columns
Send data from our Android apps to the server, receive the data and save it to the database, php code is needed. The PHP code for this app, assuming the name of the file is process.php
From the app to the data server / HTTP Post, our app needs internet access. For this, add the permissions below at Andorid Mainifests
We need Apacche HTTP library to send the HTTP request to the server. For this we will add the dependencie below to build.gradle.
dependencies {
compile ‘org.jbundle.util.osgi.wrapped: org.jbundle.util.osgi.wrapped.org.apache.http.client: 4.1.2’
}
We will put a button in our main layer, which will click on our server to post the data.
Our MainActivity Class:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
button = (Button) findViewById (R.id.button);
button.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View v) {
new UpdateTask (). execute ();
}
});
}
}
When you click on the button, we will call another class. For that we have to create a class. Right click on the package and create a new class. We gave the name: UpdateTask
UpdateTask class:
import android.os.AsyncTask;
import android.util.log;
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 java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
class UpdateTask extends AsyncTask {
protected String doInBackground (String … urls) {
HttpClient httpClient = new DefaultHttpClient ();
// replacewith your url
HttpPost httpPost = new HttpPost (“http://utopianlab.com/app/process.php”);
// Post Data
List nameValuePair = new ArrayList (2);
nameValuePair.add (new BasicNameValuePair (“name”, “Jack”));
nameValuePair.add (new BasicNameValuePair (“email”, “jack@awesomemail.com”));
// Encoding POST data
try {
httpPost.setEntity (new UrlEncodedFormEntity (nameValuePair));
} catch (UnsupportedEncodingException e) {
// log exception
e.printStackTrace ();
}
// making POST request
try {
HttpResponse response = httpClient.execute (httpPost);
// write response to log
Log.d (“Http Post Response:”, response.toString ());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace ();
} catch (IOException e) {
// Log exception
e.printStackTrace ();
}
return null;
}
}
Here we originally wrote the code to post data to the server. AsyncTask means the background process task. When we post data to the server, it will work in the background. And manually send one request to the web server after one. We will send the data according to the pair. One is the name, the other is the data. And we will put all of our data on a list, according to the pair. We have just left two data in the above code. And finally we sent our data server via HttpPost. Which will be added to the PHP database
Now if we look at the database table, then our data will be added to the database. You can see it from phpMyAdmin
Here we have added value, statically. Such as Name and Email You can try modifying the code by modifying it. For example, users can post their name and email here, add text fields to the layout file
Also, if you want to show saved data on the web site, you can easily do it via php queries. For those shared links above: If you see a simple project with php and MySQL, you can do it yourself. Thanks to everyone
Thank you so much
No comments:
Post a Comment