网络模块使用Hilt注入

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

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:name=".di.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.OkHttp"
        android:networkSecurityConfig="@xml/network_security_config">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:authorities="${applicationId}.provider"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>

</manifest>
XML 复制代码
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.1.4" apply false
}

buildscript{
    dependencies{
        classpath("com.google.dagger:hilt-android-gradle-plugin:2.28-alpha")
    }
}

依赖

Kotlin 复制代码
plugins {
    id("com.android.application")
    id("dagger.hilt.android.plugin")
}

android {
    namespace = "com.tiger.retrofel"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.tiger.retrofel"
        minSdk = 28
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

dependencies {

    //Retrofit 核心库
    implementation("com.squareup.retrofit2:retrofit:2.9.0")
    //响应数据自动序列化
    //JSON
    implementation("com.squareup.retrofit2:converter-gson:2.9.0")
    //String类型
    implementation("com.squareup.retrofit2:converter-scalars:2.9.0")
    //拦截器 logging
    implementation("com.squareup.okhttp3:logging-interceptor:3.14.+")

    //Retrofit 整合 RXjava
    implementation("com.squareup.okhttp3:adapter-rxjava2:2.9.0")

    //Hilt
    implementation("com.google.dagger:hilt-android:2.28-alpha")
    annotationProcessor("com.google.dagger:hilt-android-compiler:2.28-alpha")

    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.8.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
相关推荐
网硕互联的小客服4 分钟前
如何排查 Docker 容器资源占用过高的问题?
运维·服务器·网络·安全·docker·容器
等不到释怀22 分钟前
千人中小型校园网络的网络规划
网络·华为·毕业设计·课程设计
Johny_Zhao25 分钟前
基于CentOS Stream 8的物联网数据采集与展示方案
linux·网络·python·mqtt·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维
Python私教1 小时前
FastAPI 与 JWT 身份验证:保护你的 API
网络·fastapi
Orlando cron4 小时前
Kubernetes 网络模型深度解析:Pod IP 与 Service 的负载均衡机制,Service到底是什么?
网络·tcp/ip·kubernetes
KKKlucifer8 小时前
加密通信 + 行为分析:运营商行业安全防御体系重构
网络·安全·重构
achene_ql10 小时前
select、poll、epoll 与 Reactor 模式
linux·服务器·网络·c++
黎相思11 小时前
应用层自定义协议与序列化
运维·服务器·网络
Lightning-py12 小时前
Linux命令cat /proc/net/snmp查看网络协议层面统计信息
网络·网络协议·tcp/ip
wo32586614512 小时前
浪潮交换机配置track检测实现高速公路收费网络主备切换NQA
开发语言·网络·php