android程序编写

大哥大姐们,打扰一下,android程序编写
最新回答
对错何妨

2024-04-26 00:32:52

android向服务器post文件的步骤及实现代码:

  1. 实现将图片转换成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

发送Base64加密需要引入的包:
import android.util.Base64;

实现将图片转换成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;
}
因为直接把字符串放到Url里所以用GET的方式向服务器传参数…向服务器发送数据的方法应该会吧?…希望这些能帮助到你,满意请采纳,谢谢。
没纸就用手抠

2024-04-26 01:02:16

用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;
}