2024-04-26 00:32:52
android向服务器post文件的步骤及实现代码:
实现将图片转换成Base64加密String的类:
public String bitmaptoString(Bitmap bitmap) {
// 将Bitmap转换成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
2.用post方法,将数据发送到服务器
public static String doPost(String requestUrl, String requestData)
throws ClientProtocolException, IOException {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
HttpConnectionParams.setSoTimeout(params, TIMEOUT);
HttpClient client = new DefaultHttpClient(params);
/* 建立HTTPPost对象 */
HttpPost httpRequest = new HttpPost(requestUrl);
String strResult = null;
/* 添加请求参数到请求对象 */
StringEntity stringEntity = new StringEntity(requestData, "UTF-8");
httpRequest.setEntity(stringEntity);
httpRequest.setHeader("Charset", "UTF-8");
httpRequest.setHeader("Content-Type", "text/xml");
HttpResponse httpResponse = null;
httpResponse = client.execute(httpRequest);
if (httpResponse == null) {
return strResult;
}
return strResult;
}
2024-04-26 00:02:22
2024-04-26 01:02:16