Android Create a HTTP Request without using any library

In this blog, we will learn how to create a HTTP Request without using any library.

In Mobile App Development , we usually depend on a server and to make a connection with the server, we need to create http calls.
For this purpose we use networking libraries in our project so that we can easily create the http request for our use.
Some of the popular libraries are Volley, Retrofit etc.

Of course, these libraries help us a lot and make our work easy and our calls faster.

But what if we don’t wan to use any of these libraries.

Let us see how to do this :

APPROACH :

  1. You need to create an Async Task that will be running on background thread.
    P.S : The Android developers have restricted the HTTP Requests to be working on background thread.
    If you get to know any other method than Async Task , please let me know in the comments below.
  2. Pass on url and your data in the execute method of the Async Task you just created as arguments.
  3. In doInBackground() method of your Async Task extract the url and data from the arguments.
  4. Create a new Object of the URL class by passing the url of your HTTP Request.
  5. Create a Object of HttpURLConnection/HttpsURLConnection and assign it the value returned  by the openConnection() method of the URL object you created.
    Note : You need to type cast the value returned by openConnection() method into your desired HttpURLConnection/HttpsURLConnection Object.
  6. On this object set the request method Type (e.g POST , GET,PUT) by calling the function setRequestMethod() and passing the name of the request method as the argument.
  7. If you need to add headers to your request you can simply do so by calling the function setREquestProperty() and passing the header name as the first key and value as the second key.
  8. Get the output stream of your HttpURLConnection/HttpsURLConnection object.
  9. Create a new object of OutputStreamWriter by passing in the output stream obtained above.
  10. Write the data with the help of write function of the OutpuStreamWriter object you just created.
  11. Close the stream and close the connection as you have send the request.

CODE :

AsyncTask for the request :

Calling the Async Task :

That’s all you have created your first HTTP request without using any library.

Keep coding and Keep Sharing đŸ™‚

. . .

Comments (2)

Add Your Comment

  • F.Selin
    Hello, Last year I was using Async Task but I realized AsyncTask caused memory leaks. I decided to change this method. Right now I am searching a new method for http request without using any library. Please let me know if you know something else.
    • anchit
      Hi,

      Well AsyncTask causes memory leaks, usually, this statement itself is wrong, it’s our faulty code that causes memory leaks. Still, I will not get into the depths of this topic as i don’t know what eactly is your scenario and code.

      Regarding any other approaches, I can only suggest using FutureTask or threads for each network calls (This might give you even bigger memory leaks if not handled properly).

  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close