作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在 Android/Kotlin 中上传文件:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//...........
UploadFilesTask().execute(1)
}
//.......
private class UploadFilesTask : AsyncTask<Int, Int, Int>() {
override fun doInBackground(vararg params: Int?): Int? {
val MEDIA_TYPE_PNG = MediaType.parse("image/jpg")
val client = OkHttpClient()
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "test123")
.addFormDataPart("file", "image-name-123.jpg", RequestBody.create(MEDIA_TYPE_PNG,
File("/storage/emulated/0/Download/real-file-name.jpg")))
.build()
val request = Request.Builder()
.url("http://httpbin.org/post")
.post(requestBody)
.build()
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
throw IOException("Unexpected code " + response)
} else {
Log.d("test", "Done123")
Log.d("test", response.body().string())
}
return 123
}
protected fun onProgressUpdate(vararg progress: Int) {
}
protected override fun onPostExecute(result: Int?) {
}
}
我在运行时看到的唯一错误消息是:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
我该如何解决?
权限设置:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
和依赖:
compile "com.squareup.okhttp3:okhttp:3.0.1"
最佳答案
我测试了您的代码,它按预期工作。与我的唯一区别是我使用的是真实设备,所以我的文件路径是/storage/sdcard0/Download/real-file-name.jpg
。我建议您检查模拟器存储中是否存在该文件:
adb shell ls/storage/emulated/0/Download/real-file-name.jpg
输出应该是这样的:
-rw-rw-r-- root sdcard_rw 21577 2016-01-26 16:16 real-file-name.jpg
这是您的代码的输出示例
01-26 18:00:52.877 29568-29644/com.omainegra.sof D/test: Done123
01-26 18:00:54.383 29568-29644/com.omainegra.sof D/test: {
"args": {},
"data": "",
"files": {
"file": "data:image/jpg;base64,/9j/4AAQSkZJRgAB...
Recomendation
可以使用 RxAndroid
轻松完成更好的( react 性方法)
class MainActivity : AppCompatActivity() {
val observable = Observable.create<String> { observer ->
try {
if (!observer.isUnsubscribed) {
val MEDIA_TYPE_PNG = MediaType.parse("image/jpg")
val client = OkHttpClient()
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "test123")
.addFormDataPart("file", "image-name-123.jpg", RequestBody.create(MEDIA_TYPE_PNG,
File("/storage/sdcard0/Download/real-file-name.jpg")))
.build()
val request = Request.Builder()
.url("http://httpbin.org/post")
.post(requestBody)
.build()
val response = client.newCall(request).execute();
if (!response.isSuccessful)
throw IOException("Unexpected code " + response);
observer.onNext(response.body().string())
}
} catch (e: Exception) {
observer.onError(e);
}
}
lateinit var subscription: Subscription
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
subscription = observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
Log.d("test", "Done123")
Log.d("test", response)
}, { error ->
Log.e("test", "onError", error)
})
}
override fun onDestroy(){
super.onDestroy()
subscription.unsubscribe()
}
}
将依赖项添加到您的 build.gradle
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
关于android - 无法在 Android 中上传文件并且没有错误的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35007212/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!