码迷,mamicode.com
首页 > 移动开发 > 详细

Android 使用okhtth上传图片multipart/form-data

时间:2016-09-15 01:06:17      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:

自己整这个问题整了半天,写篇博客记录下
private void uploadMultiFile() {
String imageType = "multipart/form-data";
File file = new File(imgUrl);imgUrl为图片位置
RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "head_image", fileBody)
.addFormDataPart("imagetype", imageType)
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
final okhttp3.OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
OkHttpClient okHttpClient = httpBuilder
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {

}

@Override
public void onResponse(Response response) throws IOException {
String htmlStr = response.body().string();
Log.i("result", htmlStr);

}
});
}

Android 使用okhtth上传图片multipart/form-data

标签:

原文地址:http://www.cnblogs.com/gznuhaoge/p/5874042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!