|
| 1 | +package com.hmkcode.http |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.net.ConnectivityManager |
| 5 | +import android.net.Network |
| 6 | +import android.net.NetworkInfo |
| 7 | +import android.os.AsyncTask |
| 8 | +import android.os.Build |
| 9 | +import androidx.appcompat.app.AppCompatActivity |
| 10 | +import android.os.Bundle |
| 11 | +import android.widget.TextView |
| 12 | +import kotlinx.android.synthetic.main.activity_main.* |
| 13 | +import java.io.BufferedReader |
| 14 | +import java.io.InputStream |
| 15 | +import java.io.InputStreamReader |
| 16 | +import java.net.HttpURLConnection |
| 17 | +import java.net.URL |
| 18 | + |
| 19 | +class MainActivity : AppCompatActivity() { |
| 20 | + |
| 21 | + lateinit var tvIsConnected: TextView; |
| 22 | + lateinit var tvResult: TextView; |
| 23 | + |
| 24 | + |
| 25 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 26 | + super.onCreate(savedInstanceState) |
| 27 | + setContentView(R.layout.activity_main) |
| 28 | + tvIsConnected = findViewById<TextView>(R.id.tvIsConnected) |
| 29 | + tvResult = findViewById<TextView>(R.id.tvResult) |
| 30 | + |
| 31 | + if(checkNetworkConnection()) |
| 32 | + HTTPAsyncTask().execute("http://hmkcode-api.appspot.com/rest/api/hello/Android") |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + private fun checkNetworkConnection(): Boolean { |
| 37 | + val cm:ConnectivityManager = |
| 38 | + getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager |
| 39 | + |
| 40 | + val networkInfo:NetworkInfo? = cm.activeNetworkInfo |
| 41 | + val isConnected: Boolean = if(networkInfo != null) networkInfo.isConnected() else false |
| 42 | + |
| 43 | + if(isConnected){ |
| 44 | + tvIsConnected.setText("Connected "+networkInfo?.typeName) |
| 45 | + tvIsConnected.setBackgroundColor(0xFF7CCC26.toInt()) |
| 46 | + |
| 47 | + }else{ |
| 48 | + tvIsConnected.setText("Not Connected!") |
| 49 | + tvIsConnected.setBackgroundColor(0xFFFF0000.toInt()) |
| 50 | + } |
| 51 | + return isConnected; |
| 52 | + } |
| 53 | + |
| 54 | + inner class HTTPAsyncTask : AsyncTask<String, Void, String>() { |
| 55 | + override fun doInBackground(vararg urls: String?): String { |
| 56 | + return HttpGet(urls[0]) |
| 57 | + } |
| 58 | + override fun onPostExecute(result: String?) { |
| 59 | + tvResult.setText(result) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private fun HttpGet(myURL: String?): String { |
| 64 | + |
| 65 | + val inputStream:InputStream |
| 66 | + val result:String |
| 67 | + |
| 68 | + // create URL |
| 69 | + val url:URL = URL(myURL) |
| 70 | + |
| 71 | + // create HttpURLConnection |
| 72 | + val conn:HttpURLConnection = url.openConnection() as HttpURLConnection |
| 73 | + |
| 74 | + // make GET request to the given URL |
| 75 | + conn.connect() |
| 76 | + |
| 77 | + // receive response as inputStream |
| 78 | + inputStream = conn.inputStream |
| 79 | + |
| 80 | + // convert inputstream to string |
| 81 | + if(inputStream != null) |
| 82 | + result = convertInputStreamToString(inputStream) |
| 83 | + else |
| 84 | + result = "Did not work!" |
| 85 | + |
| 86 | + return result |
| 87 | + } |
| 88 | + |
| 89 | + private fun convertInputStreamToString(inputStream: InputStream): String { |
| 90 | + val bufferedReader:BufferedReader? = BufferedReader(InputStreamReader(inputStream)) |
| 91 | + var line:String? = bufferedReader?.readLine() |
| 92 | + var result:String = "" |
| 93 | + |
| 94 | + while (line != null) { |
| 95 | + result += line |
| 96 | + line = bufferedReader?.readLine() |
| 97 | + } |
| 98 | + |
| 99 | + inputStream.close() |
| 100 | + return result |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments