没有合适的资源?快使用搜索试试~ 我知道了~
首页android 开发中使用okhttp上传文件到服务器
资源详情
资源评论
资源推荐
android 开发中使用开发中使用okhttp上传文件到服务器上传文件到服务器
在开发android手机客户端,常常会需要上传文件到服务器,使用okhttp会是一个很好的选择,它使用很简单,而且运行效率也很高,下
面小编给大家带来了android 开发中使用okhttp上传文件到服务器功能,一起看看吧
开发android手机客户端,常常会需要上传文件到服务器,比如:你手机里的照片。
使用okhttp会是一个很好的选择。它使用很简单,而且运行效率也很高。
首先,在 app/build.gradle 的 dependencies 增加 implementation 'com.squareup.okhttp3:okhttp:3.8.1' 可以参照如下代码
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.cofox.mykt.myweather"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
res.srcDirs =
[
'src/main/res/layout/menufunction',
'src/main/res'
]
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
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'
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 'org.jetbrains.anko:anko-sdk19:0.10.3'
implementation 'org.jetbrains.anko:anko-support-v4:0.10.3'
implementation 'org.jetbrains.anko:anko-appcompat-v7:0.10.3'
implementation 'com.google.code.gson:gson:2.7'
implementation 'com.android.support:percent:26.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
}
在界面上添加一个按钮,以及一个可滚动显示返回值的文字组件。
<Button
android:id="@+id/btnOkHttpUploadFilePost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OkHttp上传文件(POST)"
android:textAllCaps="false" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ttviewResponse"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
因为只是基本功能实现,所以采用发送手机上指定的一个文件就达成目的。
在代码编辑区,首先添加一个默认的服务器地址。
//设置访问服务端IP
var serverIp = "192.168.1.105"
在onCreate方法内添加按钮操作代码
//post方式上传文件(sd卡跟路径image.png文件)
btnOkHttpUploadFilePost.setOnClickListener {
Thread {
try {
val url = "http://" + serverIp + "/upload"
val file = File("/sdcard/image.png")
val fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file)
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("uploadfile", "image.png", fileBody)
.build()
val request = Request.Builder()
weixin_38611796
- 粉丝: 8
- 资源: 944
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- stc12c5a60s2 例程
- Android通过全局变量传递数据
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0