android 開發(fā)中使用okhttp上傳文件到服務(wù)器
開發(fā)android手機(jī)客戶端,常常會(huì)需要上傳文件到服務(wù)器,比如:你手機(jī)里的照片。
使用okhttp會(huì)是一個(gè)很好的選擇。它使用很簡單,而且運(yùn)行效率也很高。
首先,在 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'
}
在界面上添加一個(gè)按鈕,以及一個(gè)可滾動(dòng)顯示返回值的文字組件。
<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>
因?yàn)橹皇腔竟δ軐?shí)現(xiàn),所以采用發(fā)送手機(jī)上指定的一個(gè)文件就達(dá)成目的。
在代碼編輯區(qū),首先添加一個(gè)默認(rèn)的服務(wù)器地址。
//設(shè)置訪問服務(wù)端IP var serverIp = "192.168.1.105"
在onCreate方法內(nèi)添加按鈕操作代碼
//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()
.url(url)
.post(requestBody)
.build()
val httpBuilder = OkHttpClient.Builder()
val okHttpClient = httpBuilder
.connectTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
.writeTimeout(15, java.util.concurrent.TimeUnit.SECONDS)
.build()
val response = okHttpClient.newCall(request).execute()
val responseStr = response.body()?.string()
runOnUiThread { ttviewResponse.text = responseStr }
} catch (e: Exception) {
}
}.start()
}
這段代碼中的 val url 值是根據(jù)服務(wù)端的要求設(shè)置的。
val file 是手機(jī)上圖片文件的位置。
val requestBody 中 .addFormDataPart("uploadfile", "image.png", fileBody) 的 uploadfile 也是服務(wù)端要求的必要鍵。
最后的 responseStr 是上傳操作之后,獲取服務(wù)端的信息反饋。
總結(jié)
以上所述是小編給大家介紹的android 開發(fā)中使用okhttp上傳文件到服務(wù)器,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android自定義View實(shí)現(xiàn)遙控器按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)遙控器按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android開發(fā)之選項(xiàng)卡功能的實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android開發(fā)之選項(xiàng)卡功能的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android選項(xiàng)卡功能的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06
android基礎(chǔ)總結(jié)篇之九:Intent應(yīng)用詳解
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之九:Intent應(yīng)用詳解,有需要的可以了解一下。2016-11-11
Android自定義控件單位尺寸實(shí)現(xiàn)代碼
這篇文章主要介紹了Android自定義控件單位尺寸實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Android實(shí)現(xiàn)可折疊式標(biāo)題欄
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可折疊式標(biāo)題欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android實(shí)現(xiàn)文字上下滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文字上下滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android獲取WebView加載url的請(qǐng)求錯(cuò)誤碼 【推薦】
這篇文章主要介紹了Android獲取WebView加載url的請(qǐng)求錯(cuò)誤碼 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果
這篇文章主要為大家詳細(xì)介紹了AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

