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 :
- 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. - Pass on url and your data in the execute method of the Async Task you just created as arguments.
- In doInBackground() method of your Async Task extract the url and data from the arguments.
- Create a new Object of the URL class by passing the url of your HTTP Request.
- 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. - 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.
- 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.
- Get the output stream of your HttpURLConnection/HttpsURLConnection object.
- Create a new object of OutputStreamWriter by passing in the output stream obtained above.
- Write the data with the help of write function of the OutpuStreamWriter object you just created.
- Close the stream and close the connection as you have send the request.
CODE :
AsyncTask for the request :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
private class MyHttpRequestTask extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... params) { String my_url = params[0]; String my_data = params[1]; try { URL url = new URL(my_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // setting the Request Method Type httpURLConnection.setRequestMethod("POST"); // adding the headers for request httpURLConnection.setRequestProperty("Content-Type", "application/json"); try{ //to tell the connection object that we will be wrting some data on the server and then will fetch the output result httpURLConnection.setDoOutput(true); // this is used for just in case we don't know about the data size associated with our request httpURLConnection.setChunkedStreamingMode(0); // to write tha data in our request OutputStream outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); outputStreamWriter.write(my_data); outputStreamWriter.flush(); outputStreamWriter.close(); // to log the response code of your request Log.d(ApplicationConstant.TAG, "MyHttpRequestTask doInBackground : " +httpURLConnection.getResponseCode()); // to log the response message from your server after you have tried the request. Log.d(ApplicationConstant.TAG, "MyHttpRequestTask doInBackground : " +httpURLConnection.getResponseMessage()); }catch (Exception e){ e.printStackTrace(); }finally { // this is done so that there are no open connections left when this task is going to complete httpURLConnection.disconnect(); } }catch (Exception e){ e.printStackTrace(); } return null; } } |
Calling the Async Task :
1 2 3 |
String my_url = "http://192.168.15.107/mylocalserver";// Replace this with your own url String my_data = "Hello my First Request Without any library";// Replace this with your data new MyHttpRequestTask().execute(my_url,my_data); |
That’s all you have created your first HTTP request without using any library.
Keep coding and Keep Sharing 🙂