AndroidOkhttp3上传单个文件
生活随笔
收集整理的這篇文章主要介紹了
AndroidOkhttp3上传单个文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
implementation 'com.squareup.okhttp3:okhttp:3.10.0'implementation 'com.squareup.okio:okio:1.12.0'
忽略https請求:
res->xml->network_security_config.xml
清單文件中添加:
<application android:networkSecurityConfig="@xml/network_security_config" application/>上傳文件代碼
OkHttpClient client = new OkHttpClient();MediaType type = MediaType.parse("application/octet-stream");//“text/xml;charset=utf-8”File file1 = new File(path + fileName);RequestBody fileBody = RequestBody.create(type, file1);Request request = new Request.Builder().url("http://192.168.0.107:8087/fileupload/fileupload/").addHeader("User-Agent", "android").header("Content-Type", "text/html; charset=utf-8;").post(fileBody).build();client.newCall(request).enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {e.printStackTrace();}@Overridepublic void onResponse(Call call, Response response) throws IOException {runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(Main2Activity.this, "成功", Toast.LENGTH_SHORT).show();}});}});總結
以上是生活随笔為你收集整理的AndroidOkhttp3上传单个文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: System.err: java.net
- 下一篇: cf对电脑配置要求有哪些?