kotlin通过协程来实现异步操作,关于协程的一些基本概念可以参考这里Kotlin中线程的代替者——协程,究竟有什么好?. kotlin在它的标准库中仅仅提供了最小化的底层API的支持。像async和await既不是Kotlin的关键字,又没有被包括在标准库中。 对于一般的开发者,或是刚入门的的人来说仅仅通过标准库提供的那些东西,要想使...
Retrofit是流行的RESTful HTTP网络请求框架,基于OkHttp,简化网络请求接口封装。支持多种请求方法、请求头、参数及响应格式注解,适用于Android和Kotlin协程,实现高效网络通信,解决HTTPS代理请求问题。
import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import retrofit2.create object RetrofitClient { val url = "https:///"; private val instance: Retrofit by lazy { Retrofit.Builder().client(OkHttpClient.Builder().build()).baseUrl(url) .addConverterFactory(GsonConverterF...
了解基本的 Kotlin 相关知识。 1. 添加依赖 在app-module 的 build.gradle 文件中添加依赖: dependencies { // retrofit compile "com.squareup.retrofit2:retrofit:2.3.0" compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0" compile "com.squareup.retrofit2:converter-gson:2.3.0" // rxandroid comp...
和Retrofit一样,需要定义一个接口类 其中@AutoApi,@AutoFlowApi,@NetStrategy是自定义的注解,后面会介绍到。 2.要先编译,会在你的接口类的文件夹下生成一个xxxRepository.class 这是通过注解自动生成的文件,使用了kotlinpoet 并且这里apiService就是通过Retrofit拿到的接口代理 ...
网络是任何 Android 应用程序的主要组件之一。有很多很好的库可以帮助我们轻松地建立网络。此类库的一些示例包括 Retrofit 和FastAndroidNetworking。 在本篇文章中,我将为您提供使用 Retrofit 与 RxJava 和 Kotlin 构建 Android 应用程序所需的基本设置和概念。
package com.example.app.entityclassUserBean { } 在http中封装接口请求基本回调抽象类 package com.example.app.http import android.util.Log import android.widget.Toast import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.internal.EverythingIsNonNull ...
Kotlin中的Retrofit 2 Kotlin代码是非常类似我们在Java中用的。我们将看到更多的细节有哪些不同,而且,你会发现这一切都是很简单和直观的。 你还会看到我们也将创建一些非常实用的扩展函数。 构建build.gradle 我在这里不会解释太多,而你需要将下面指令加入到build.gradle中: ...
Kotlin中的Retrofit 2 Kotlin代码是非常类似我们在Java中用的。我们将看到更多的细节有哪些不同,而且,你会发现这一切都是很简单和直观的。 你还会看到我们也将创建一些非常实用的扩展函数。 构建build.gradle 我在这里不会解释太多,而你需要将下面指令加入到build.gradle中: ...
Retrofit的项目主页地址是: https://github.com/square/retrofit 添加依赖 implementation 'com.squareup.retrofit2:retrofit:2.9.0' 添加网络权限 <uses-permission android:name="android.permission.INTERNET" /> Retrofit基本使用 1.根据Http接口创建kotlin接口 ...