标签:android style http java 使用 os io 文件
android中的网络操作和java里面没有什么区别,java里面的很多网络操作方法都可以搬到android中去使用,主要几个点:
1、post和get请求的区别,大家可以在网上查阅有关资料进行了解,get主要以向地址中拼接字符串参数发送到服务器,长度有限制,并且请求参数暴露在地址栏中,不怎么安全;post则主要是将请求参数转换为相应的http协议请求体发送到服务器,相比get方式,参数的长度没有限制,并且参数信息不会暴露给用户;
2、我们在java web里面通过浏览器以post方式发送数据,浏览器帮我们转换为相应的http协议,也就是说浏览器内部帮我们设置为相应的http请求体,然后发送到服务器,用户通过浏览器向服务器发送数据-->浏览器转换为http协议-->发送到服务器,如果我们用android做客户端用java代码编写post请求,需要设置其请求体,而get方式请求则只需要拼接相应的参数地址,用户通过android客户端向服务器发送数据-->编码设置为相应的http协议-->发送到服务器;
3、android里面包含了apache的httpclient,相当与web里面的浏览器,我们还可以利用其api进行相应的请求操作,相比与纯java代码,它里面封装了很多东西,我们只需要根据业务需求选择相应的api即可;如果我们在swing中使用apache的httpclient,则需要导入其相应的jar包
post请求:
java web:用户通过浏览器向服务器发送数据-->浏览器转换为http协议-->发送到服务器
java代码:用户通过android客户端向服务器发送数据-->编码设置为相应的http协议-->发送到服务器
httpclient:用户通过android客户端向服务器发送数据(httpclient帮我们转换为相应的http协议)-->发送到服务器
代码实现功能:里面都有相应的注释,我在我的工程里面建立了一个专门处理Http上传、下载的类:HttpUtil
1、从服务器获取二进制数据,图片、视频等:
/** * 从服务器上获取二进制数据,例如图片、视频等 * @param serverUrl: 服务器地址 * @param newFileName:获取到本地后新文件的地址+名称 例如:C://test.png */ public static void getByteFromServer(String serverUrl,String newFileName)throws Exception{ URL url = new URL(serverUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET");//通过Get方式请求 conn.setConnectTimeout(5 * 1000);//设置连接延迟,因为android系统内存有限,不能长时间去与服务器建立连接 InputStream inStream = conn.getInputStream();//通过输入流获取图片数据 byte[] data = readInputStream(inStream);//得到图片的二进制数据 File imageFile = new File(newFileName); FileOutputStream outStream = new FileOutputStream(imageFile); outStream.write(data); outStream.close(); }
/** * 将输入流转换为字节数据 * @param inStream * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=inStream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); }
/** * 从服务器上获取文本信息,例如html、xml等信息 * @param serverUrl * @return 文本字符串 */ public static String getTxtFromServer(String serverUrl)throws Exception{ URL url = new URL(serverUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();//通过输入流获取txt数据 byte[] data = readInputStream(inStream);//得到txt的二进制数据 String txt = new String(data); return txt; }
/**Get请求方式即是通过将参数放到地址中发送到服务器 * 向服务器发送Get请求 * @param path:服务器请求地址 * @param params:请求参数 * @param enc:编码 * @return true:请求成功 false:请求失败 * @throws Exception */ public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{ StringBuilder sb = new StringBuilder(path); //?username=jack&password=123456&age=23 if(params!=null && !params.isEmpty()){ sb.append('?'); for(Map.Entry<String, String> entry : params.entrySet()){ sb.append(entry.getKey()).append('=') .append(URLEncoder.encode(entry.getValue(), enc)).append('&'); } sb.deleteCharAt(sb.length()-1); } URL url = new URL(sb.toString()); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); if(conn.getResponseCode()==200){ //请求成功,可以调用上面的getByteFromServer方法得到相应的服务器数据 //byte[] b=readInputStream(conn.getInputStream()); return true; } return false; }
/**Post请求在浏览器中是转换了相应的http协议进行 * 而转换后的http协议中包含了例如Content-Type、Content-Length等信息 * 所以通过android客户端发送post请求需要将其设置为相应的http协议发送出去 * 向服务器发送一个post表单请求 * @param path:服务器地址 * @param params:请求参数 * @param enc:编码 * @return true:请求成功 false:请求失败 * @throws Exception */ public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{ //username=jack&password=123456&age=23 StringBuilder sb = new StringBuilder(); if(params!=null && !params.isEmpty()){ for(Map.Entry<String, String> entry : params.entrySet()){ sb.append(entry.getKey()).append('=') .append(URLEncoder.encode(entry.getValue(), enc)).append('&'); } sb.deleteCharAt(sb.length()-1); } byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5 * 1000); conn.setUseCaches(false);//不进行缓存 conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据 //设置http请求头,向服务器发送post请求,Content-Type和Content-Length两个参数必须要,其他可省略 //设置发送内容类型,为表单数据 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置发送内容长度 conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length)); conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); conn.setRequestProperty("Connection", "Keep-Alive"); OutputStream outStream = conn.getOutputStream(); outStream.write(entitydata); outStream.flush(); outStream.close(); if(conn.getResponseCode()==200){ //请求成功,可以调用上面的getByteFromServer方法得到相应的服务器数据 //byte[] b=readInputStream(conn.getInputStream()); return true; } return false; }
/** * 向服务器发送xml数据 * @param path:服务器地址 * @param xml:xml字符串信息 * @param enc:编码 * @return true:请求成功 false:请求失败 * @throws Exception */ public static boolean sendPostXMLRequest(String path, String xml,String enc)throws Exception{ byte[] data = xml.getBytes(); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5 * 1000); conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据 //设置发送内容类型,为xml数据 conn.setRequestProperty("Content-Type", "text/xml; charset="+enc); //设置发送内容长度 conn.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outStream = conn.getOutputStream(); outStream.write(data); outStream.flush(); outStream.close(); if(conn.getResponseCode()==200){ //byte[] b=readInputStream(conn.getInputStream()); return true; } return false; }
/** * 通过httpClient发送get请求 * @param path:服务器地址 * @param params:参数名称 * @param enc:编码 * @return true:请求成功 false:请求失败 * @throws Exception */ public static boolean sendGetRequestFromHttpClient(String path, Map<String, String> params,String enc) throws Exception{ StringBuilder sb = new StringBuilder(path); //?username=jack&password=123456&age=23 if(params!=null && !params.isEmpty()){ sb.append('?'); for(Map.Entry<String, String> entry : params.entrySet()){ sb.append(entry.getKey()).append('=') .append(URLEncoder.encode(entry.getValue(), enc)).append('&'); } sb.deleteCharAt(sb.length()-1); } //相当与浏览器 HttpClient httpClient=new DefaultHttpClient(); //得到一个HttpGet对象 HttpGet get=new HttpGet(sb.toString()); //发送一个Get请求 HttpResponse response=httpClient.execute(get); if(response.getStatusLine().getStatusCode()==200){ //String str=EntityUtils.toString(response.getEntity());即可得到服务器数据信息 //InputStream inStream=httpEntity.getContent();即可得到服务器输入流 return true; } return false; }
/** * 通过httpClient发送Post请求 * @param path:服务器地址 * @param params:参数名称 * @param enc:编码 * @return true:请求成功 false:请求失败 * @throws Exception */ public static boolean sendPostRequestFromHttpClient(String path, Map<String, String> params,String enc) throws Exception{ List<NameValuePair> paramPairs = new ArrayList<NameValuePair>(); if(params!=null && !params.isEmpty()){ for(Map.Entry<String, String> entry : params.entrySet()){ paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据 HttpPost post = new HttpPost(path); //form post.setEntity(entitydata); DefaultHttpClient client = new DefaultHttpClient(); //浏览器 HttpResponse response = client.execute(post);//执行请求 if(response.getStatusLine().getStatusCode()==200){ //String str=EntityUtils.toString(response.getEntity());即可得到服务器数据信息 //InputStream inStream=httpEntity.getContent();即可得到服务器输入流 return true; } return false; }
建立表单实体文件:
public class FormFile { /* 上传文件的数据 */ private byte[] data; private InputStream inStream; private File file; /* 文件名称 */ private String filname; /* 请求参数名称*/ private String parameterName; /* 内容类型 */ private String contentType = "application/octet-stream"; public FormFile(String filname, byte[] data, String parameterName, String contentType) { this.data = data; this.filname = filname; this.parameterName = parameterName; if(contentType!=null) this.contentType = contentType; } public FormFile(String filname, File file, String parameterName, String contentType) { this.filname = filname; this.parameterName = parameterName; this.file = file; try { this.inStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } if(contentType!=null) this.contentType = contentType; } public File getFile() { return file; } public InputStream getInStream() { return inStream; } public byte[] getData() { return data; } public String getFilname() { return filname; } public void setFilname(String filname) { this.filname = filname; } public String getParameterName() { return parameterName; } public void setParameterName(String parameterName) { this.parameterName = parameterName; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } }
/** * 向服务器提交的信息包含上传文件信息 * 直接通过HTTP协议提交数据到服务器,实现如下面表单提交功能: * <FORM METHOD=POST ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data"> <INPUT TYPE="text" NAME="name"> <INPUT TYPE="text" NAME="id"> <input type="file" name="imagefile"/> <input type="file" name="zip"/> </FORM> * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试) * @param params 请求参数 key为参数名,value为参数值 * @param file 上传文件 */ public static boolean sendPostRequest(String path, Map<String, String> params, FormFile[] files) throws Exception{ final String BOUNDARY = "---------------------------7da2137580612"; //数据分隔线 final String endline = "--" + BOUNDARY + "--\r\n";//数据结束标志 int fileDataLength = 0; for(FormFile uploadFile : files){//得到文件类型数据的总长度 StringBuilder fileExplain = new StringBuilder(); fileExplain.append("--"); fileExplain.append(BOUNDARY); fileExplain.append("\r\n"); fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n"); fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n"); fileExplain.append("\r\n"); fileDataLength += fileExplain.length(); if(uploadFile.getInStream()!=null){ fileDataLength += uploadFile.getFile().length(); }else{ fileDataLength += uploadFile.getData().length; } } StringBuilder textEntity = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) {//构造文本类型参数的实体数据 textEntity.append("--"); textEntity.append(BOUNDARY); textEntity.append("\r\n"); textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n"); textEntity.append(entry.getValue()); textEntity.append("\r\n"); } //计算传输给服务器的实体数据总长度 int dataLength = textEntity.toString().getBytes().length + fileDataLength + endline.getBytes().length; URL url = new URL(path); int port = url.getPort()==-1 ? 80 : url.getPort(); Socket socket = new Socket(InetAddress.getByName(url.getHost()), port); OutputStream outStream = socket.getOutputStream(); //下面完成HTTP请求头的发送 String requestmethod = "POST "+ url.getPath()+" HTTP/1.1\r\n"; outStream.write(requestmethod.getBytes()); String accept = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n"; outStream.write(accept.getBytes()); String language = "Accept-Language: zh-CN\r\n"; outStream.write(language.getBytes()); String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "\r\n"; outStream.write(contenttype.getBytes()); String contentlength = "Content-Length: "+ dataLength + "\r\n"; outStream.write(contentlength.getBytes()); String alive = "Connection: Keep-Alive\r\n"; outStream.write(alive.getBytes()); String host = "Host: "+ url.getHost() +":"+ port +"\r\n"; outStream.write(host.getBytes()); //写完HTTP请求头后根据HTTP协议再写一个回车换行 outStream.write("\r\n".getBytes()); //把所有文本类型的实体数据发送出来 outStream.write(textEntity.toString().getBytes()); //把所有文件类型的实体数据发送出来 for(FormFile uploadFile : files){ StringBuilder fileEntity = new StringBuilder(); fileEntity.append("--"); fileEntity.append(BOUNDARY); fileEntity.append("\r\n"); fileEntity.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n"); fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n"); outStream.write(fileEntity.toString().getBytes()); if(uploadFile.getInStream()!=null){ byte[] buffer = new byte[1024]; int len = 0; while((len = uploadFile.getInStream().read(buffer, 0, 1024))!=-1){ outStream.write(buffer, 0, len); } uploadFile.getInStream().close(); }else{ outStream.write(uploadFile.getData(), 0, uploadFile.getData().length); } outStream.write("\r\n".getBytes()); } //下面发送数据结束标志,表示数据已经结束 outStream.write(endline.getBytes()); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); if(reader.readLine().indexOf("200")==-1){//读取web服务器返回的数据,判断请求码是否为200,如果不是200,代表请求失败 return false; } outStream.flush(); outStream.close(); reader.close(); socket.close(); return true; }
Android之旅十五 android中的网络操作,布布扣,bubuko.com
标签:android style http java 使用 os io 文件
原文地址:http://blog.csdn.net/harderxin/article/details/38356461