-
-
Save JillevdW/a1c5c4c293fe2365e85dda50fbfe25f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HttpGetRequest : AsyncTask<String, Void, String>() { | |
| val REQUEST_METHOD = "GET" | |
| val READ_TIMEOUT = 15000 | |
| val CONNECTION_TIMEOUT = 15000 | |
| override fun doInBackground(vararg params: String?): String { | |
| val stringUrl : String = params[0] as String | |
| var result : String? = null | |
| var inputLine : String? = null | |
| try { | |
| val url = URL(stringUrl) | |
| var connection : HttpURLConnection = url.openConnection() as HttpURLConnection | |
| Log.d("CURRENT", connection.toString()) | |
| connection.requestMethod = REQUEST_METHOD | |
| connection.readTimeout = READ_TIMEOUT | |
| connection.connectTimeout = CONNECTION_TIMEOUT | |
| connection.connect() | |
| var streamReader = InputStreamReader(connection.inputStream) | |
| var reader = BufferedReader(streamReader) | |
| var stringBuilder = StringBuilder() | |
| while ({inputLine = reader.readLine(); inputLine}() != null) { | |
| stringBuilder.append(inputLine) | |
| } | |
| reader.close() | |
| streamReader.close() | |
| result = stringBuilder.toString() | |
| } catch (e : Throwable) { | |
| e.printStackTrace() | |
| result = "ERROR" | |
| } | |
| return result as String | |
| } | |
| override fun onPostExecute(result: String?) { | |
| super.onPostExecute(result) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment