開發(fā)android手機客戶端,常常會需要上傳文件到服務器,比如:你手機里的照片。
創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為武勝等服務建站,武勝等地企業(yè),進行企業(yè)商務咨詢服務。為武勝企業(yè)網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。
使用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>因為只是基本功能實現,所以采用發(fā)送手機上指定的一個文件就達成目的。
在代碼編輯區(qū),首先添加一個默認的服務器地址。
//設置訪問服務端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()
.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 值是根據服務端的要求設置的。
val file 是手機上圖片文件的位置。
val requestBody 中 .addFormDataPart("uploadfile", "image.png", fileBody) 的 uploadfile 也是服務端要求的必要鍵。
最后的 responseStr 是上傳操作之后,獲取服務端的信息反饋。
總結
以上所述是小編給大家介紹的android 開發(fā)中使用okhttp上傳文件到服務器,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網站的支持!
分享名稱:android開發(fā)中使用okhttp上傳文件到服務器
標題鏈接:http://www.chinadenli.net/article2/gpsioc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、網站設計、網站營銷、品牌網站制作、商城網站、網站維護
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)