Send Data From Android Device To Php Server

In this tutorial, we will show you how android device and PHP server will communicate with each other using httpclient or httppost method. When you are going to implement your idea into an android app you need a server where you will store DATA & get DATA from server. These ‘DATA’ could be user profile, last purchase details, users activity, time, data, images etc. In this tutorial we will first create an login form for user. After submit button press user’s data like username , lastname , password and time wi send to our PHP server database. Summary steps to develop an android client and PHP server communication application :

1) Create User login Form in android app.
2) Get form data using MainActivity.class(value holder class).
3) Create Database table to store user details.
4) Create PHP server to accept data from android client.
5) Run App in Real Device using USB Debugging.

Android client send Data to PHP server

Login & Download source code

download-code350-100

 




Create User Login Form in Android App

Create new android project in android studio. Go to File–> New –> New Project and give any name to your app (Mine was SendDataToPHP). Now on activity_main.xml create four EditView, One TextView. one Spinner & one Button.

EditView-

User can write any text in this box. In our case user will type username, password, phone number & hint answer

Textbox-

Display any text to user like ” Create login “.

Spinner-

A Dropdown list is called Spinner in android. In our case we will put some questions in this list

senddata login form

























activity_main.xml

Get Form Data Using MainActivity.class(value holder class)

Now We have form layout ready & we will get all form data value using MainActivity.class file & will send data to PHP server in next step. First declare all TextView ,EditView, Spinner & button above onCreate(Bundle savedInstanceState) method. I hope you already know how define EditView with EditView Id mention in activity_main.xml file. For beginners i write these 2 lines. Inside onCreate() connect EditView to EditView id define in activity_main.xml file(like R.id.user_name).

If your login form contains security questions as a dropdown list(Spinner). See last line in above code i have added spinner for hint questions. To add questions in spinner you have to create a ArrayList object. Add all questions to ArrayList & Finally add ArrayList to spinner using default Adapter called ArrayAdapter.

Now when user press submit button- first check whether all field are empty or not. Second check which language (Keyboard language- English, Hindi, etc.) your server suppose to be support. Here i am considering first case only- check all field are none empty. Once it is clear that user filled all the required fields properly then we need a ArrayList that holder all data which will be pass to AsyncTask. AsyncTask is use for all network related task. AsyncTask execute on separate thread in background not in Main thread. Android OS does not allow network task to be execute on main thread.

AsyncTask have three sections.

1) onPreExecute() – prepare task related functions like show progress bar to user.
2) doInBackground() – perform background task like send request to server.
3) onPostExecute() – present server response to user screen on main thread.

Complete MainActivity.class Code

Inside doInBackground(ArrayList… alldata) we will again retrieve value from mylist( ArrayList ).Because we are using ArrayList which is like “phonenumber” =” 123456″ where phonenumber is ID for 123456. These IDs are used in PHP to fetch each data value. So we have to assign an ID to each data value. Remember NameValuePair method is included in httpclient library. So make sure to add httpclient dependency on gradle file(app module). Otherwise it show “can’t resolved method” error.

Create Database table to store user details.

Now we will do Server side coding and table creating task. First we will create SQL database table using PHPMyAdmin. Please watch below mansion video. Follow all the step describe in this video. Remember There are two type of hosting provider -1) Window hosting 2) Linux Hosting.I am using Linux hosting which provides CPanal. If you are using windows hosting, Database & table creation task is same for both type of Hosting plan windows & linux. If you are going to purchase hosting i will suggest you to buy Linux hosting. CPanel providers lot of option as you require for website development.

 

Create Database & table using PHPmyAdmin

 

Create PHP Page to accept data from android client.

Now create php page which will receive data from android app. The name of php file will StoreUserData.php. See android code where user data are send to this URL “http://blueappsoftware.in/android/tutorial/StoreUserData.php. In StoreUserData.php file first store all value in a variable. Then use simple SQL INSERT query for storing data into table.

StoreUserData.php

Run App in Real Device using USB Debugging

There you can see then user type username, password & other details and hit submit button request sent to php server page. In Response to request from user PHP server reply with username & password which user has written in android app form.

 

Run code in Emulator OR in Real Device

 

Login & Download source code

download-code350-100

 




Download Source Code

* indicates required



Posted in andorid.

kamal bunkar

I Am Post Graduation (M.Tech. C.S.E) from VNIT, Nagpur. I Have Published Two Research Paper On International Journal. My Apps on Google Play Store- Sanyukt Parivar(Mp Police ), NOTOtag- Digital Chhindwara, The Tubby-VoiceRecognizer & More.
Contact me at kamal.bunkar@blueappsoftware.in

33 Comments

  1. I love your blog.. very nice colors & theme. Did you create this
    website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to create my own blog and would like to find
    out where u got this from. appreciate it

  2. Pretty great post. I just stumbled upon your weblog and wanted to say
    that I’ve truly loved browsing your blog posts.
    In any case I’ll be subscribing for your feed and I hope you write once more very soon!

  3. Hi there! This post could not be written any better!
    Reading through this post reminds me of my good old room mate!
    He always kept talking about this. I will forward this article to him.
    Fairly certain he will have a good read. Thank you for sharing!

  4. Hi there! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly?
    My weblog looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might
    be able to fix this issue. If you have any recommendations, please share.
    Thank you!

  5. This is really attention-grabbing, You are an overly skilled blogger.
    I have joined your feed and look forward to in the hunt
    for extra of your great post. Also, I have shared your site in my social networks

  6. I’m really enjoying the theme/design of your blog.

    Do you ever run into any browser compatibility issues?

    A few of my blog audience have complained about my site not
    working correctly in Explorer but looks great in Chrome.
    Do you have any solutions to help fix this issue?

  7. I was recommended this web site by my cousin. I’m
    not sure whether this post is written by him as no one else
    know such detailed about my problem. You are wonderful!

    Thanks!

  8. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

  9. Having read this I thought it was very informative. I appreciate you finding the time and
    energy to put this short article together. I once again find myself personally spending a lot of time both reading and posting comments.
    But so what, it was still worthwhile!

  10. hi! may i know is your code complete? because i run the program and does nothing, i have download the given source code.

    • Yes this code is working…You have to create file on php server to store FCM key into your database for future use. First check on your android studio log whether FCM token is printing OR not. If it is printing in log than send token to your web server.

  11. Good day! I could have sworn I’ve been to
    this website before but after going through
    a few of the articles I realized it’s new to me.

    Nonetheless, I’m certainly delighted I found it and I’ll be book-marking it and checking back regularly!

  12. Spot on with this write-up, I absolutely feel this website needs much more attention. I’ll probably be returning to read
    through more, thanks for the info!

  13. Hey very cool website!! Man .. Excellent ..
    Amazing .. I will bookmark your site and take the feeds additionally?
    I am glad to search out numerous useful info here
    in the submit, we need work out more strategies
    on this regard, thanks for sharing. . . . . .

  14. Pingback: ExoPlayer Android Example- Best Alternative of VideoView By Kamal

  15. Pingback: Simplest way to get WordPress Post in Android App- REST API

  16. I just like the helpful information you supply to your articles.

    I’ll bookmark your blog and test once more here frequently.
    I’m fairly sure I will learn many new stuff proper here!
    Good luck for the following!

Comments are closed.