Skip to content

Instantly share code, notes, and snippets.

@ahmadmust8
Last active December 14, 2017 00:16
Show Gist options
  • Save ahmadmust8/f62f257f62f740f5c0b0aa0d3c87513f to your computer and use it in GitHub Desktop.
Save ahmadmust8/f62f257f62f740f5c0b0aa0d3c87513f to your computer and use it in GitHub Desktop.
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
import java.nio.charset.Charset
import java.text.SimpleDateFormat
import java.util.Date
/**
* Created by ahmad on 14/12/17.
*/
object Utils {
/** content of :
* readFromStream method
* formatDate method
* formatTime method
* createUrl method
* makeHttpRequest method
*/
/**
* Return the formatted date string (i.e. "Mar 3, 1984") from a Date object.
*/
fun formatDate(dateObject: Date): String {
val dateFormat = SimpleDateFormat("LLL dd, yyyy")
return dateFormat.format(dateObject)
}
/**
* Return the formatted date string (i.e. "4:30 PM") from a Date object.
*/
fun formatTime(dateObject:Date):String {
val timeFormat = SimpleDateFormat("h:mm a")
return timeFormat.format(dateObject)
}
/**
* Returns new URL object from the given string URL.
*/
private fun createUrl(stringUrl: String): URL? {
var url: URL? = null
try {
url = URL(stringUrl)
} catch (e: MalformedURLException) {
// TODO Handel the Exception.
}catch (e :Exception){
// TODO Handel the Exception.
}
return url
}
/**
* Make an HTTP request to the given URL and return a String as the response.
*/
private fun makeHttpRequest(url: URL?): String {
var jsonResponse = ""
// If the URL is null, then return early.
if (url == null) {
return jsonResponse
}
var urlConnection: HttpURLConnection? = null
var inputStream: InputStream? = null
try {
urlConnection = url.openConnection() as HttpURLConnection
urlConnection.setReadTimeout(10000 /* milliseconds */)
urlConnection.setConnectTimeout(15000 /* milliseconds */)
urlConnection.setRequestMethod("GET")
urlConnection.connect()
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.responseCode === 200) {
inputStream = urlConnection.getInputStream()
jsonResponse = readFromStream(inputStream)
} else {
// TODO : there is a problem with the response code
}
} catch (e: IOException) {
// TODO Handel the Exception.
} catch (e :Exception){
// TODO Handel the Exception.
}finally {
if (urlConnection != null) {
urlConnection.disconnect()
}
if (inputStream != null) {
// Closing the input stream could throw an IOException, which is why
// the makeHttpRequest(URL url) method signature specifies than an IOException
// could be thrown.
inputStream.close()
}
}
return jsonResponse
}
/**
* Convert the [InputStream] into a String which contains the
* whole JSON response from the server.
*/
private fun readFromStream(inputStream: InputStream?): String {
val output = StringBuilder()
if (inputStream != null) {
val inputStreamReader = InputStreamReader(inputStream, Charset.forName("UTF-8"))
val reader = BufferedReader(inputStreamReader)
var line = reader.readLine()
while (line != null) {
output.append(line)
line = reader.readLine()
}
}
return output.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment