开发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' }