2023-03-11

【Kotlin】以 OkHttp3 偵測是否有網際網路連線能力

參考資料 ----

OkHttp


OkHttp 是輕量型的 3rd party library,這篇筆記只是記錄透過 OkHttp 偵測 Android 裝置是否有連上網際網路。

上網比較了幾個網站,選定目標網站是網頁內容(html 字數,包含 CSS, javascript...) 最少的 example.com


app 層級的 build.gradle

  1.  
  2. ...
  3. ...
  4.  
  5. android {
  6. ...
  7. ...
  8. buildFeatures {
  9. viewBinding true
  10. }
  11. buildTypes {
  12. ...
  13. ...
  14. }
  15. }
  16.  
  17. ...
  18. ...
  19.  
  20. dependencies {
  21. ...
  22. ...
  23. implementation 'com.squareup.okhttp3:okhttp:4.7.2'
  24. }
  25.  



AndroidManifest.xml
  1.  
  2.  
  3. <uses-permission android:name="android.permission.INTERNET"/>
  4.  
  5. <application
  6. ...
  7. ...
  8.  



activity_main.xml
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <androidx.constraintlayout.widget.ConstraintLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. xmlns:tools="http://schemas.android.com/tools"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10.  
  11. <TextView
  12. android:id="@+id/lblHello"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Hello World!"
  16. app:layout_constraintBottom_toBottomOf="parent"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. app:layout_constraintStart_toStartOf="parent"
  19. app:layout_constraintTop_toTopOf="parent" />
  20.  
  21. <Button
  22. android:id="@+id/btnCheck"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="check internet"
  26. app:layout_constraintBottom_toBottomOf="parent"
  27. app:layout_constraintEnd_toEndOf="parent"
  28. app:layout_constraintHorizontal_bias="0.064"
  29. app:layout_constraintStart_toStartOf="parent"
  30. app:layout_constraintTop_toTopOf="parent"
  31. app:layout_constraintVertical_bias="0.023"
  32. android:onClick="onBtnClicked"/>
  33.  
  34. </androidx.constraintlayout.widget.ConstraintLayout>
  35.  



MainActivity.kt
  1.  
  2. ...
  3. ...
  4.  
  5. import android.os.Bundle
  6. import android.util.Log
  7. import android.view.View
  8. import androidx.appcompat.app.AppCompatActivity
  9. import com.example.okhp.databinding.ActivityMainBinding
  10. import okhttp3.*
  11. import okio.IOException
  12.  
  13.  
  14. class MainActivity : AppCompatActivity() {
  15.  
  16. val TAG = "MyTag"
  17. private lateinit var binding: ActivityMainBinding
  18. private lateinit var client: OkHttpClient
  19.  
  20. override fun onCreate(savedInstanceState: Bundle?) {
  21. super.onCreate(savedInstanceState)
  22. binding = ActivityMainBinding.inflate(layoutInflater)
  23. val view = binding.root
  24. setContentView(view)
  25. }
  26.  
  27.  
  28. fun onBtnClicked(vv: View) {
  29. val client = OkHttpClient()
  30. val request = Request.Builder()
  31. .url("https://example.com")
  32. .build()
  33.  
  34. client.newCall(request).enqueue(object : Callback {
  35. override fun onResponse(call: Call, response: Response) {
  36. // Log.d(TAG, "response: ${response.body?.string()}")
  37. runOnUiThread {
  38. binding.lblHello.text = "connected"
  39. }
  40. }
  41.  
  42. override fun onFailure(call: Call, e: IOException) {
  43. // Log.d(TAG, e.toString())
  44. runOnUiThread {
  45. binding.lblHello.text = "no internet"
  46. }
  47. }
  48. })
  49. }
  50. }
  51.  



Volley 稍微比較了一下,OkHttp3 速度確實快了一些。


相關筆記 ----


沒有留言:

張貼留言