1.服务端采用的是.net的WEBAPI接口。2.android多文件上传。以下为核心代码: 1 package com.example.my.androidupload; 2 3 4 import android.util.Log; 5 6 import java.io. ...
1.服务端采用的是.net的WEBAPI接口。
2.android多文件上传。
以下为核心代码:
1 package com.example.my.androidupload; 2 3 4 import android.util.Log; 5 6 import java.io.File; 7 import java.io.Serializable; 8 import java.util.Iterator; 9 import java.util.LinkedHashMap; 10 import java.util.Map; 11 import java.util.Map.Entry; 12 13 import org.apache.http.Consts; 14 import org.apache.http.HttpEntity; 15 import org.apache.http.HttpResponse; 16 import org.apache.http.client.HttpClient; 17 import org.apache.http.client.methods.HttpPost; 18 import org.apache.http.entity.ContentType; 19 import org.apache.http.entity.mime.MultipartEntityBuilder; 20 import org.apache.http.entity.mime.content.FileBody; 21 import org.apache.http.entity.mime.content.StringBody; 22 import org.apache.http.impl.client.DefaultHttpClient; 23 import org.apache.http.util.EntityUtils; 24 25 /** 26 * Created by wz on 2016/04/19. 27 */ 28 public class HttpClientUtil { 29 public final static String Method_POST = "POST"; 30 public final static String Method_GET = "GET"; 31 32 /** 33 * multipart/form-data类型的表单提交 34 * 35 * @param form 表单数据 36 */ 37 public static String submitForm(MultipartForm form) { 38 // 返回字符串 39 String responseStr = ""; 40 41 // 创建HttpClient实例 42 /* HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); 43 CloseableHttpClient httpClient = httpClientBuilder.build();*/ 44 // CloseableHttpClient httpClient= HttpClients.createDefault(); 45 46 HttpClient httpClient = new DefaultHttpClient(); 47 try { 48 // 实例化提交请求 49 HttpPost httpPost = new HttpPost(form.getAction()); 50 51 // 创建MultipartEntityBuilder 52 MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); 53 54 Log.e("HttpClientUtil", "111"); 55 // 追加普通表单字段 56 Map<String, String> normalFieldMap = form.getNormalField(); 57 for (Iterator<Entry<String, String>> iterator = normalFieldMap.entrySet().iterator(); iterator.hasNext(); ) { 58 Entry<String, String> entity = iterator.next(); 59 entityBuilder.addPart(entity.getKey(), new StringBody(entity.getValue(), ContentType.create("text/plain", Consts.UTF_8))); 60 } 61 Log.e("HttpClientUtil", "222"); 62 // 追加文件字段 63 Map<String, File> fileFieldMap = form.getFileField(); 64 for (Iterator<Entry<String, File>> iterator = fileFieldMap.entrySet().iterator(); iterator.hasNext(); ) { 65 Entry<String, File> entity = iterator.next(); 66 entityBuilder.addPart(entity.getKey(), new FileBody(entity.getValue())); 67 } 68 Log.e("HttpClientUtil", "333"); 69 // 设置请求实体 70 httpPost.setEntity(entityBuilder.build()); 71 Log.e("HttpClientUtil", "444"); 72 // 发送请求 73 HttpResponse response = httpClient.execute(httpPost); 74 int statusCode = response.getStatusLine().getStatusCode(); 75 // 取得响应数据 76 HttpEntity resEntity = response.getEntity(); 77 Log.e("HttpClientUtil", "结果:" + Integer.toString(statusCode)); 78 if (200 == statusCode) { 79 if (resEntity != null) { 80 responseStr = EntityUtils.toString(resEntity); 81 } 82 } 83 } catch (Exception e) { 84 System.out.println("提交表单失败,原因:" + e.getMessage()); 85 } finally { 86 try { 87 httpClient.getConnectionManager().shutdown(); 88 } catch (Exception ex) { 89 } 90 } 91 return responseStr; 92 } 93 94 /** 95 * 表单字段Bean 96 */ 97 public class MultipartForm implements Serializable { 98 /** 99 * 序列号100 */101 private static final long serialVersionUID = -2138044819190537198L;102 103 /**104 * 提交URL105 **/106 private String action = "";107 108 /**109 * 提交方式:POST/GET110 **/111 private String method = "POST";112 113 /**114 * 普通表单字段115 **/116 private Map<String, String> normalField = new LinkedHashMap<String, String>();117 118 /**119 * 文件字段120 **/121 private Map<String, File> fileField = new LinkedHashMap<String, File>();122 123 public String getAction() {124 return action;125 }126 127 public void setAction(String action) {128 this.action = action;129 }130 131 public String getMethod() {132 return method;133 }134 135 public void setMethod(String method) {136 this.method = method;137 }138 139 public Map<String, String> getNormalField() {140 return normalField;141 }142 143 public void setNormalField(Map<String, String> normalField) {144 this.normalField = normalField;145 }146 147 public Map<String, File> getFileField() {148 return fileField;149 }150 151 public void setFileField(Map<String, File> fileField) {152 this.fileField = fileField;153 }154 155 public void addFileField(String key, File value) {156 fileField.put(key, value);157 }158 159 public void addNormalField(String key, String value) {160 normalField.put(key, value);161 }162 }163 164 165 }
原标题:Android实现表单提交,webapi接收
关键词:Android
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。