Android中用xutil去实现http操作。
android xutil form-data upload file
Multipart/form-data construction with android – Stack Overflow
android上传大文件到服务器客户端和服务器端代码 – lvshaorong的博客 – 博客频道 – CSDN.NET
Android使用XUtils框架上传照片(一张或多张)和文本,服务器使用struts2接收 – 程序园
private String uploadHost=”http://192.168.1.100:8080/ReceiveImgFromAndroid/sendMessage_messageAction.action”; //服务器接收地址 private List<imgPath> imgList; //存放图片地址 RequestParams params=new RequestParams(); params.addBodyParameter(“msg”,”上传图片”); for(int i = 0; i < imgList.size(); i++){ //这里拼成【attachment[i]】的形式,并加上【multipart/form-data】属性 params.addBodyParameter(“attachment[” + i + “]”, new File(imgList.get(i)), “multipart/form-data”); } sendImgToServer(params,uploadPath); public void uploadMethod(final RequestParams params,final String uploadHost) { http.send(HttpRequest.HttpMethod.POST, uploadHost, params,new RequestCallBack<String>() { @Override public void onStart() { //上传开始 } @Override public void onLoading(long total, long current,boolean isUploading) { //上传中 } @Override public void onSuccess(ResponseInfo<String> responseInfo) { //上传成功,这里面的返回值,就是服务器返回的数据 //使用 String result = responseInfo.result 获取返回值 } @Override public void onFailure(HttpException error, String msg) { //上传失败 } }); } |
android快速开发框架xUtils – Android应用开发 云豆网,北大青鸟官方学员社区
RequestParams params = new RequestParams(); params.addHeader(“name”, “value”); params.addQueryStringParameter(“name”, “value”); // 只包含字符串参数时默认使用BodyParamsEntity, // 类似于UrlEncodedFormEntity(”application/x-www-form-urlencoded”)。 params.addBodyParameter(“name”, “value”); // 加入文件参数后默认使用MultipartEntity(”multipart/form-data”), // 如需”multipart/related”,xUtils中提供的MultipartEntity支持设置subType为”related”。 // 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如: // MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。 // 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset)); params.addBodyParameter(“file”, new File(“path”)); … HttpUtils http = new HttpUtils(); http.send(HttpRequest.HttpMethod.POST, “uploadUrl….”, params, new RequestCallBack<String>() { @Override public void onStart() { testTextView.setText(“conn…”); } @Override public void onLoading(long total, long current, boolean isUploading) { if (isUploading) { testTextView.setText(“upload: ” + current + “/” + total); } else { testTextView.setText(“reply: ” + current + “/” + total); } } @Override public void onSuccess(ResponseInfo<String> responseInfo) { testTextView.setText(“reply: ” + responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { testTextView.setText(error.getExceptionCode() + “:” + msg); } }); |