http://blog.csdn.net/sanyang730/article/details/51317083
http://blog.csdn.net/moniteryao/article/details/52683691
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
private final OkHttpClient client = new OkHttpClient();
private void uploadImg() {
// mImgUrls为存放图片的url集合
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (int i = 0; i <mImgUrls.size() ; i++) {
File f=new File(mImgUrls.get(i));
if (f!=null) {
builder.addFormDataPart("img", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
}
}
//添加其它信息
// builder.addFormDataPart("time",takePicTime);
// builder.addFormDataPart("mapX", SharedInfoUtils.getLongitude());
// builder.addFormDataPart("mapY",SharedInfoUtils.getLatitude());
// builder.addFormDataPart("name",SharedInfoUtils.getUserName());
MultipartBody requestBody = builder.build();
//构建请求
Request request = new Request.Builder()
.url(Constant.BASE_URL)//地址
.post(requestBody)//添加请求体
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("上传失败:e.getLocalizedMessage() = " + e.getLocalizedMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("上传照片成功:response = " + response.body().string());
ToastCustom.makeText(PictureListActivity.this, "上传成功", Toast.LENGTH_LONG).show();
}
});
}