Send data to remote server in android using Volley library

Send data to remote server in android using Volley library

In this tutorial, we will learn, how to send the data to the remote server in an android application using Volley Library. Android app will perform Volley’s POST request to send data. we are using volley Library because it reduces the complexity to fetch or post data over the network.the main advantage of volley library is, the developer doesn’t need to write lengthy code for accessing the network.We will use WAMP server to communicate with the remote database.

Step:-1 Configuring Remote Server

  • Download WAMP Server on your PC. Install it and run the Wamp server. if Wamp server logo turns its color to green its ready to use.

  • Now Open the PHPMyAdmin by hitting “http://localhost/phpmyadmin/” in your browser. The username will be “root” and password will be blank. Click “GO”.
  • Create a Database name it “check”, then create a table inside check database. name it userdata.

  • Table column name will be id, name, email, password as shown in below image.

Step:-2 Creating PHP file

  • Open www folder inside C drive > wamp> www.
  • Create a folder inside www. name it user.
  • Inside user folder, create a file name it index.php.
  • Open Index.php file in Notepad++ Editor. and add the following code inside it.make sure file type must be PHP. and save it.

Step 3: – Creating Project & adding Dependencies

Open your Android Studio -> Go to File > New > New project -> Choose BlankActivity > Finish.

Open your app level build.gradle and add following Volley Library dependencies

Step 4: – Add Internet permission 

Add Internet permission in your AndroidManifest.xml file.

Step 5: – Add Layout File

Create or add an XML file inside layout folder name it activity_main.xml. this file contains three edit text which holds the value of name, email, password and a button which submit the details to remote server.

Step 6: – Creating Server Url 

  • Open Command Prompt and Type “ipconfig” you will get ipv4 Address as shown in below image.

  • Here my server URL will be

Step 7: –  Create MainActivity.java file

  • Inside MainActivity.java file declare server URL as a string.
  • Create RequestQueue which is used to hold the HTTP Requests. pass the context in RequestQueue.
  • HTTP Requests where the response is parsed as a  String. For a POST Request, to add the parameter, a MAP Needs to return and getparam() method needs to be overridden. we will add the name, email, password to Map in the form of key value pair inside getparam() method to send the data to remote server.

 

Final MainActivity.java will look like this

Step 7: –  Run Application 

Now run the application and open table inside phpmyadmin you can see thats user records has been saved successfully.

 

4 سال پیش

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

این سایت از اکیسمت برای کاهش هرزنامه استفاده می کند. بیاموزید که چگونه اطلاعات دیدگاه های شما پردازش می‌شوند.