Android网络访问之Retrofit使用教程

作者:懒到死的程序员 时间:2023-08-30 01:02:08 

一、概念

HttpClientAndroid 6中移除(API数量多扩展困难)。
HttpURLConnection目前官方集成的。
OKHttpSquare公司出品,底层通讯的实现。
RetrofitSquare公司出品,上层接口的封装,更方便使用面向对象思维进行网络操作。

二、使用

Android 9开始默认只允许使用 HTTPS 类型的网络请求,HTTP明文传输因为有安全隐患不再支持。坚持使用的话需要配置:右键res目录→New→Directory→创建一个xml目录,右键xml目录→New→File→创建一个network_config.xml文件,修改内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
           <certificates src="system" />
       </trust-anchors>
   </base-config>
</network-security-config>

Manifest {
    //添加网络访问权限
    <uses-permission android:name="android.permission.INTERNET" />
    //允许HTTP访问
    <application
        android:networkSecurityConfig="@xml/network_config"
    </application>
}

2.1HttpURLConnection

thread {
   var connection: HttpURLConnection? = null
   try {
       val response = StringBuilder()
       val url = URL("https://www.baidu.com")
       connection = url.openConnection() as HttpURLConnection
       connection.connectTimeout = 8000
       connection.readTimeout = 8000
       //GET请求
       val input = connection.inputStream
       val reader = BufferedReader(InputStreamReader(input))
       reader.useLines { response.append(it) }
       print(response.toString())
       //POST请求
       connection.requestMethod = "POST"
       val output = DataOutputStream(connection.outputStream)
       output.writeBytes("username=admin&password=123456")
   } catch (e: Exception) {
       e.printStackTrace()
   } finally {
       connection?.disconnect()
   }
}

2.2OKHttp

传送门

2.3Retrofit

查看最新版本

implementation 'com.squareup.retrofit2:retrofit:2.9.0'    //会连带下载 OkHttp和Okio
implementation 'com.squareup.retrofit2:converter-gson:2.k6.1'    //会连带下载 GSON

2.3.1 定义实体类

根据 JSON 内容,编写对应的实体类。

data class Person(var name: String, var age: Int)

2.3.2 定义API接口

根据 API 接口,编写对应的访问文件。命名通常以功能名称开头+Service结尾。

@GET从服务器获取数据
@POST向服务器提交数据
@PUT @PATCH修改服务器上的数据
@DELETE删除服务器上的数据
interface PersonService {
   //接口1:https://www.baidu.com/person.json
   @GET("person.json")    //表示发起的是GET请求,传入请求的地址(相对路径,重复根路径在后面配置)
   fun getPerson(): Call<list<Person>>    //返回值必须声明成Retrofit内置的Call类型,通过泛型指定服务器返回的具体数据类型
   //接口2:https://www.baidu.com/<page>/person.json
   @GET("{page}/get_data.json")    //使用 {page} 占位
   fun getData(@Path("page") page: Int): Call<Data>    //使用 @Path("page")注解来声明对应参数
   //接口3:https://www.baidu.com/person.json?u=<user>&t=<token>
   @GET("person.json")
   fun getData(@Query("u") user: String, @Query("t") token: String): Call<Data>
   //接口4:https://api.caiyunapp.com/v2/place?query=北京&token={token}&lang=zh_CN
   @GET("v2/place?token=${GlobalApplication.TOKEN}&lang=zh_CN")    //不变的参数固定写在GET里
   fun searchPlaces(@Query("query") query: String): Call<PlaceResponse>
   //接口5:https://www.baidu.com/data/<id>
   @DELETE("data/{id}")
   fun deleteData(@Path("id") id: String): Call<ResponseBody>    //该泛型表示能接受任意类型切不会进行解析
   //接口6:https://www.baidu.com/data/create{"id": 1, "content": "The description for this data."}
   @POST("data/create")
   fun createData(@Body data: Data): Call<ResponseBody>    //将Data对象中的数据转换成JSON格式的文本,并放到HTTP请求的body部分
   //接口7:http://example.com/get_data.json
   //      User-Agent: okhttp    //header参数就是键值对
   //      Cache-Control: max-age=0
   //静态声明
   @Headers("User-Agent: okhttp", "Cache-Control: max-age=0")
   @GET("get_data.json")
   fun getData(): Call<Data>
   //动态声明
   @GET("get_data.json")
   fun getData(@Header("User-Agent") userAgent: String, @Header("Cache-Control") cacheControl: String): Call<Data>
}

2.3.3 构建Retrofit对象

val retrofit = Retrofit.Builder()
   .baseUrl("https://www.baidu.com/")    //配置重复的根路径
   .addConverterFactory(GsonConverterFactory.create())    //指定解析数据使用的转换库(这里是Gson)
   .build()

2.3.4 创建API接口实例并调用访问函数

//创建 * 对象
val personService = retrofit.create(PersonService::class.java)
//调用访问函数
personService.getPerson().enqueue(object : Call<List<person>> {    //根据注解中配置的地址进行网络请求
   override fun onResponse(call: Call<List<person>>, response: Response<List<person>>) {
       val list = response.body()    //得到解析后的对象
   }
   override fun onFailure(call: Call<List<person>>, t: Trouble) {
       t.printStackTrace()
   }
})

2.3.5 优化

object GlobalRetrofit {
   private const val BASE_URL = "www.baidu.com/"
   val retrofit: Retrofit = Retrofit.Builder()
       .baseUrl(BASE_URL)
       .addConverterFactory(GsonConverterFactory.create())
       .build()
   //fun <T> create(serviceClass: Class<T>): T = retrofit.create(serviceClass)
   inline fun <reified T> create(): T = create(T::class.java)
}
//使用
val personService = GlobalRetrofit.create<PersonService>()

来源:https://blog.csdn.net/HugMua/article/details/127762176

标签:Android,Retrofit,网络访问
0
投稿

猜你喜欢

  • Java实现五子棋的基础方法

    2021-07-11 12:32:08
  • Java sleep方法及中断方式、yield方法代码实例

    2023-10-22 10:59:49
  • C#正则表达式的6个简单例子

    2022-12-15 04:50:16
  • 聊聊Spring——AOP详解(AOP概览)

    2023-11-01 04:44:18
  • Android 吸入动画效果实现分解

    2021-10-24 16:55:28
  • java 交换两个数据的方法实例详解

    2021-12-06 00:56:04
  • springBoot详解集成Swagger流程

    2023-02-26 22:23:33
  • 使用java的HttpClient实现多线程并发

    2022-09-19 20:35:55
  • Java 获取Web项目相对webapp地址的实例

    2022-07-03 17:46:00
  • Unity游戏之存储数据

    2022-06-06 20:58:28
  • Java实现时间日期格式转换示例

    2023-06-06 04:45:11
  • 使用idea+gradle编译spring5.x.x源码分析

    2022-05-13 15:13:31
  • 基于WPF实现绘制地铁路线图

    2023-09-08 13:23:12
  • springcloud干货之服务注册与发现(Eureka)

    2021-07-08 11:04:23
  • C#采用mouse_event函数实现模拟鼠标功能

    2023-07-07 16:09:54
  • Java Socket编程服务器响应客户端实例代码

    2023-08-09 03:25:51
  • unity中点击某一个按钮播放某一个动作的操作

    2023-12-01 04:38:05
  • Spring Cloud Alibaba使用Nacos作为注册中心和配置中心

    2021-07-15 18:18:42
  • springcloud eureka切换nacos的配置方法

    2022-05-19 01:58:47
  • Android自定义SwipeRefreshLayout高仿微信朋友圈下拉刷新

    2023-01-06 08:51:34
  • asp之家 软件编程 m.aspxhome.com