Android HTTP GET/POST Request

In this project I'm going to show you how to perform the HTTP GET and POST Requests on almost any website with an Android application.

Step 1: Install Android Studio

Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. To download Android Studio click here.

After downloading Android studio, run the .exe file. Here are the directions from the Google Developers website:
To set up Android Studio on Windows:

  • Launch the .exe file you just downloaded.
  • Follow the setup wizard to install Android Studio and any necessary SDK tools.
  • On some Windows systems, the launcher script does not find where Java is installed. If you encounter this problem, you need to set an environment variable indicating the correct location.To do that follow these simple steps: Select Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and add a new system variable JAVA_HOME that points to your JDK folder, for example C:\Program Files\Java\jdk1.7.0_21.

Then you need to download JAVA JDK. Either 64 or 32 bits It depends on your system. If it is compatible with 32 bits download Windows X86 and if it is compatible with 64 bits download Windows X64.

Make sure you keep all the default settings for stuff you are installing so Android Studio knows where to find it. When you install the JAVA JDK make sure to memorise or write down the "Destination Folder" since you are going to need it when installing Android Studio.

After installing JAVA JDK you can now proceed to install Android Studio.Sometimes Windows 7 has an issue when finding the JDK even though you tell it exactly where it is, so here is one trick you can do that worked for me:

View the Error, then push back, then push Next and Next again and it will work.

You can leave the other parameters as they are and then you are good to go Android developer :D :D

Step 2: Basic Look Into HTTP

For those who are not familliar with Hyper Text Transfer Protocol "HTTP". HTTP is the underlying protocol used by the World Wide Web. HTTP defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands. For example, when you enter a URL in your browser, this actually sends an HTTP command to the Web server directing it to fetch and transmit the requested Web page.

The other main standard that controls how the World Wide Web works is HTML, which covers how Web pages are formatted and displayed. Communicating with HTTP servers must be using certain kind of request methods listed below:

  • GET: method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
  • HEAD: Same as GET, but transfers the status line and header section only.

  • POST : submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

  • PUT: Replaces all current representations of the target resource with the uploaded content.

  • DELETE: Removes all current representations of the target resource given by a URI.

  • CONNECT: Establishes a tunnel to the server identified by a given URI.

  • OPTIONS: Describes the communication options for the target resource.

  • TRACE: Performs a message loop-back test along the path to the target resource.

In our project today we are just going to perform the GET and POST requests.So let's Start ..

Step 3: HTTP GET Request

So to make an Android application that performs automatically the GET request to the desired website, we need to start an application project in Android studio. To do that you can either select "Start a new Android Studio Project" from the Android setup wizard or you can select "File > New Project".Then choose your application name and the Project location.After that you need to select the minimum SDK, well it depends on your device, but keep in mind that if the selected "minimum sdk" is higher than your device's SDK the app won't work .

Then leave the other parameters as they are and then you should wait for a few seconds while the software is building the project then you should see a screen as in the picture above.

So, my example app is going to be about retrieving the number of "Views" and the number of "Favorites" that I have collected for one of my instructables articles from the HTML source code of the page and displaying it on the device's screen.

The first you wanna do is to go to the Android manifest.xml file and add "android.permission.INTERNET" since we are going to make the HTTP request using internet connection which is obvious.

Second you need to setup a TextView in activity_main.xml that you are going to use to display the HTML source code of the specified website and a button that when we click on it, the app displays the code.Then you also need to declare a new varible "TextView" in MainActivity.java and associate it with the textview in the activity_main using its ID.

Now we get to the bumpy and hard part which is performing the HTTP GET Request. I'm not going to explain how it works because it is going to be a very long instructables article and you can find plenty of explainations out there just google it !!

To download the full project click :

Step 4: HTTP POST Request

The POST Request is a little bit different from the GET Request. You can perform an HTTP GET Request on technically any website but you can't do so with the POST Request since you need sort of like a password that allows you to post on that website which we will demonstrate below.

So first we need to create our own website and to do that there are plenty of free domain and hosting websites. I personally chose https://www.000webhost.com/ which sounds pretty much convenient for our simple task.

You can create a free website using their free subdomain service then just follow their instructions and you should receive an email that lists your FTP Username and FTP Password and your website will be fully functional within 24 hours.

After you will actually need a software that allows you to send the files (php, HTML , images ...) from your computer to the host servers and to do that I used FileZilla you can download it from here.

Before sending any file we need to make a file, so in this example we need to make a php file that we will name httppost.php using NotePad ++ ,which you can download from here, (check the image above for the code and a little explaination).After creating your php server file you are going to actually send that file to the servers that host your website and this is where FileZilla comes in,to accomplish that we first need to establish a connection between your computer and the host servers then you right-click on the file that you wish to send ,in this case httppost.php, and then transfer.

Now after setting up our website it's time to make the app that will send the HTTP POST Request . To do that we need to start a new project "I already described it in the GET Request". So first we need to add the INTERNET permission in AndroidManifest.xml just like in the GET Request and then we are going to make an EditText which will allow us to type the data that we wish to send and a pushButton .

To download the full project :

Step 5: Thanks for Following !!

Thanks guys for following my project , I wish it was helpful for you and don't forget to press the Follow button for more Upcoming projects. Also, if you have any questions, troubles or issues just let me know in the comment section below or email me : Khalilmerchaoui@gmail.com :D :D :D

Share

    Recommendations

    • Games Contest

      Games Contest
    • Sew Tough Challenge

      Sew Tough Challenge
    • Planter Challenge

      Planter Challenge

    Discussions