Before I already share a tutorial how to Retrieve Data from Server and Local by using UrlConnection since the HttpGet & HttpPost already deprecated.
But for today, I gonna share how to use third party network request, like Ion and OkHttp.
Let's start from URLConnection, the class that added since API 1.
Snippet Code :
to using URLConnection you need to know the protocol of the url either it is 'HTTP' or 'HTTPS' because each protocol handle by different class (HttpURLConnection / HttpsURLConnection).
Below is the result using URLConnection,
Then we move to third-party library,
Ion, overall is a good library but if you open the issue tab on github, this library face many issue with SSL (HTTPS) protocol [Even I facing with that issue].
Snippet Code :
As you can see, by using ion library the code are shortest than previous code and you didn't need to handle the url like URLConnection does. This library integrate with GSON that allow you to set raw data and get the result in POJO and also have progress for download and upload file, cool right!
Now, the last library be discuss in this post is OkHttp.
I just started using this library last month, after I facing SSL issue with Ion library.
This library created by Square
Snippet Code :
the code still shortest than using URLConnection but not short as by using Ion. This library will throw exception if there have a body but using method such get that actually doesn't have body. But overall it also a good library.
To view full project code on github, please click on this link, RequestLibrary | GitHub
And below is the sample output for each technique I explained above.
URLConnection :
Ion :