成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

android開發(fā)中使用okhttp上傳文件到服務(wù)器

開發(fā)android手機客戶端,常常會需要上傳文件到服務(wù)器,比如:你手機里的照片。

創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為武勝等服務(wù)建站,武勝等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為武勝企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

使用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>

因為只是基本功能實現(xiàn),所以采用發(fā)送手機上指定的一個文件就達成目的。

在代碼編輯區(qū),首先添加一個默認的服務(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 是手機上圖片文件的位置。

val requestBody 中 .addFormDataPart("uploadfile", "image.png", fileBody) 的 uploadfile 也是服務(wù)端要求的必要鍵。

最后的 responseStr 是上傳操作之后,獲取服務(wù)端的信息反饋。

android 開發(fā)中使用okhttp上傳文件到服務(wù)器 

總結(jié)

以上所述是小編給大家介紹的android 開發(fā)中使用okhttp上傳文件到服務(wù)器,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

分享名稱:android開發(fā)中使用okhttp上傳文件到服務(wù)器
標(biāo)題鏈接:http://jinyejixie.com/article2/gpsioc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計網(wǎng)站營銷品牌網(wǎng)站制作、商城網(wǎng)站、網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
临汾市| 抚顺市| 江口县| 万盛区| 田林县| 乐山市| 青铜峡市| 长治县| 丹江口市| 凤台县| 中超| 承德县| 北碚区| 于都县| 金川县| 亳州市| 桦川县| 虹口区| 拉萨市| 奉贤区| 吴川市| 和平区| 漳平市| 南投市| 贡山| 安陆市| 德格县| 凭祥市| 六盘水市| 昌吉市| 伊吾县| 秀山| 迭部县| 剑阁县| 正阳县| 白银市| 昆山市| 留坝县| 加查县| 通许县| 安泽县|