如何成为Android开发高手
2010-08-31 00:43:00 来源:WEB开发网04
05
06
07public class StreamTool {
08
09/**
10
11* 从输入流获取数据
12
13* @param inputStream
14
15* @return
16
17* @throws Exception
18
19*/
20
21 public static byte[] readInputStream(InputStream inputStream) throws Exception {
22
23 byte[] buffer = new byte[1024]; //你可以根据实际需要调整缓存大小
24
25 int len = -1;
26
27 ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
28
29 while( (len = inputStream.read(buffer)) != -1 ){
30
31 outSteam.write(buffer, 0, len);
32
33 }
34
35 outSteam.close();
36
37 inputStream.close();
38
39 return outSteam.toByteArray();
40
41 }
42
43}
2) 通过Android客户端上传数据到服务器:可以上传简单的表单,也可以方便的上传带有附件的文件,此类远远比Android自身的HttpClient更高效、更易于使用:
view sourceprint?001import java.io.DataOutputStream;
002
003import java.io.InputStream;
004
005import java.net.HttpURLConnection;
006
007import java.net.URL;
008
009import java.net.URLEncoder;
010
011import java.util.ArrayList;
012
013import java.util.List;
014
015import java.util.Map;
016
017
018
019import org.apache.http.HttpResponse;
020
021import org.apache.http.NameValuePair;
022
023import org.apache.http.client.HttpClient;
024
025import org.apache.http.client.entity.UrlEncodedFormEntity;
更多精彩
赞助商链接