• 网络模块使用Hilt注入


    retrofit的异步回调方法已经做了线程切换,切换到了主线程

    1. "1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    3. <uses-permission android:name="android.permission.INTERNET"/>
    4. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    6. <application
    7. android:name=".di.MyApplication"
    8. android:allowBackup="true"
    9. android:icon="@mipmap/ic_launcher"
    10. android:label="@string/app_name"
    11. android:roundIcon="@mipmap/ic_launcher_round"
    12. android:supportsRtl="true"
    13. android:theme="@style/Theme.OkHttp"
    14. android:networkSecurityConfig="@xml/network_security_config">
    15. <activity
    16. android:name=".MainActivity"
    17. android:exported="true">
    18. <intent-filter>
    19. <action android:name="android.intent.action.MAIN" />
    20. <category android:name="android.intent.category.LAUNCHER" />
    21. intent-filter>
    22. activity>
    23. <provider
    24. android:authorities="${applicationId}.provider"
    25. android:name="androidx.core.content.FileProvider"
    26. android:exported="false"
    27. android:grantUriPermissions="true">
    28. <meta-data
    29. android:name="android.support.FILE_PROVIDER_PATHS"
    30. android:resource="@xml/provider_paths"/>
    31. provider>
    32. application>
    33. manifest>
    1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
    2. plugins {
    3. id("com.android.application") version "8.1.4" apply false
    4. }
    5. buildscript{
    6. dependencies{
    7. classpath("com.google.dagger:hilt-android-gradle-plugin:2.28-alpha")
    8. }
    9. }

    依赖

    1. plugins {
    2. id("com.android.application")
    3. id("dagger.hilt.android.plugin")
    4. }
    5. android {
    6. namespace = "com.tiger.retrofel"
    7. compileSdk = 34
    8. defaultConfig {
    9. applicationId = "com.tiger.retrofel"
    10. minSdk = 28
    11. targetSdk = 34
    12. versionCode = 1
    13. versionName = "1.0"
    14. testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    15. }
    16. buildTypes {
    17. release {
    18. isMinifyEnabled = false
    19. proguardFiles(
    20. getDefaultProguardFile("proguard-android-optimize.txt"),
    21. "proguard-rules.pro"
    22. )
    23. }
    24. }
    25. compileOptions {
    26. sourceCompatibility = JavaVersion.VERSION_1_8
    27. targetCompatibility = JavaVersion.VERSION_1_8
    28. }
    29. }
    30. dependencies {
    31. //Retrofit 核心库
    32. implementation("com.squareup.retrofit2:retrofit:2.9.0")
    33. //响应数据自动序列化
    34. //JSON
    35. implementation("com.squareup.retrofit2:converter-gson:2.9.0")
    36. //String类型
    37. implementation("com.squareup.retrofit2:converter-scalars:2.9.0")
    38. //拦截器 logging
    39. implementation("com.squareup.okhttp3:logging-interceptor:3.14.+")
    40. //Retrofit 整合 RXjava
    41. implementation("com.squareup.okhttp3:adapter-rxjava2:2.9.0")
    42. //Hilt
    43. implementation("com.google.dagger:hilt-android:2.28-alpha")
    44. annotationProcessor("com.google.dagger:hilt-android-compiler:2.28-alpha")
    45. implementation("androidx.appcompat:appcompat:1.6.1")
    46. implementation("com.google.android.material:material:1.8.0")
    47. implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    48. testImplementation("junit:junit:4.13.2")
    49. androidTestImplementation("androidx.test.ext:junit:1.1.5")
    50. androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    51. }

  • 相关阅读:
    第二篇 如何选择操作系统
    正则表达式
    AWS-Lambda之导入自定义包-pip包
    电气工程师必学------CODESYS v3.5 入门学习笔记(一)
    Golang反射修改变量值
    一个基于百度飞桨封装的.NET版本OCR工具类库 - PaddleOCRSharp
    工作这么久了,你还没有重构过代码?
    打包和部署Java应用程序:Maven和Shell脚本的实用方法
    Django 日志配置解析
    ZYNQ之路--HLS入门实例
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/136631612