Android Retrofit2和Kotlin结合上传文件

本文介绍了如何在Android中结合Retrofit2和Kotlin进行文件上传操作。首先,文章提到了Retrofit2和RxJava2在Android开发中的流行,并指出在Android Studio 3.0之后Kotlin成为默认语言。接着,详细阐述了集成Retrofit2的步骤,包括编写Kotlin接口、配置请求地址、定义返回值模型以及正确携带上传参数的方法。特别强调了在上传文件时,参数必须以特定格式传递。
摘要由CSDN通过智能技术生成

现在Retrofit2和RxJava2是很流行的框架,可能是我太闭塞的缘故,竟然没有用过这样的框架,请原谅我以前都是在用xUtils3

今天就来说一下Retrofit2和Kotlin结合上传文件
1、首先集成工具
(1)Kotlin就不用说了,Android Studio 3.0 以后自身就有
(2)Retrofit2集成 在项目文件build.gradle 里添加

**dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
}**

2、
(1)编写一个KtvApiService接口

 /**
 * 作者:Galen on 2017/12/16 13:56
 * 邮箱:1270430761@qq.com
 */
interface KtvApiService {
@Multipart
@POST(FACE_DETECT)
fun detectFace(
        @Part("api_key") api_key: RequestBody,
        @Part("api_secret") api_secret: RequestBody ,
        @Part("return_attributes") return_attributes: RequestBody ,
        @Part part: MultipartBody.Part ): Call<DetectFaceResult>
@FormUrlEncoded
@POST(FACE_SET_GETDETAIL)
fun getFaceSetDetail(@Field("api_key") api_key: String,
                     @Field("api_secret") api_secret: String,
                     @Field("outer_id
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值