网络模块使用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")
}
相关推荐
Chris _data43 分钟前
S7-1500 常用工业通信协议详解与对比
运维·服务器·网络
zmj3203241 小时前
ISO/SAE 21434:2021(道路车辆 - 网络安全工程) 汇总
网络·安全·web安全·21434
IP老炮不瞎唠1 小时前
Scrapy 高效采集:优化方案与指南
网络·爬虫·python·scrapy·安全
zmj3203242 小时前
UNR -155 Annex 5提示的威胁及其编号
网络·安全·网络安全
前端摸鱼匠2 小时前
【AI大模型春招面试题14】前馈网络(FFN)在Transformer中的作用?为何其维度通常大于注意力维度?
网络·人工智能·ai·面试·大模型·transformer
ayt0072 小时前
Netty 4.2核心类解析:SingleThreadIoEventLoop的设计哲学与实现
java·网络
婷婷_1722 小时前
【PCIe 验证每日学习・Day22】PCIe 拓扑结构与 Switch / 桥片转发全解析
网络·学习·程序人生·芯片·pcie·pcie学习·pcie 拓扑
Vis-Lin2 小时前
BLE 协议栈:HCI ACL 数据详解
网络·物联网·网络协议·蓝牙·iot·ble
桌面运维家2 小时前
服务器RAID优化:IOPS性能与数据安全实战指南
运维·服务器·网络
TechWayfarer2 小时前
攻防对抗:利用IP段归属查询快速定位攻击源并联动防火墙(iptables/UFW)自动封禁
网络·网络协议·tcp/ip