day11
登录界面的http实现与数据库的交互 包含数据的提交和数据的查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package Home.HttpUtil; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; public class HttpUtil { private static final String TAG ="HttpUtil" ; public static ByteArrayOutputStream sendPost(String requrl, String reqdata){ //post请求获取二进制流 HttpURLConnection connection=null; try { requrl=requrl+"?"+reqdata; System.out.println(requrl+"yyyyyy"); URL url=new URL(requrl); connection=(HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); // 3. 指定content-type -实际上就是指定传输的数据类型 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Charset", "utf-8"); // 发送POST请求必须设置如下两行 connection.setDoInput(true); connection.setDoOutput(true); // 获取URLConnection对象对应的输出流 System.out.println(reqdata+"yyyyyy"); //OutputStream outputStream = connection.getOutputStream(); //DataOutputStream out = new DataOutputStream(connection.getOutputStream()); //outputStream.write(reqdata.getBytes("utf-8")); //PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8")); //out.write(reqdata.getBytes()); // flush输出流的缓冲 //outputStream.flush(); //outputStream.close(); //out.flush(); //out.close(); connection.connect();// 建立实际的连接 int respcode=connection.getResponseCode(); if(respcode==200){ InputStream in=connection.getInputStream(); byte []buff=new byte[1024]; int len=0; ByteArrayOutputStream rs=new ByteArrayOutputStream(); while ((len=in.read(buff))!=-1){ rs.write(buff,0,len); } in.close(); rs.close(); connection.disconnect(); Log.d(TAG,"请求成功,状态码:"+respcode); return rs; }else{ Log.d(TAG,"请求错误,状态码:"+respcode); } } catch (IOException e) { e.printStackTrace(); } return null; } public static String sendPostImg(String actionUrl, Map<String, File> files) throws IOException { String BOUNDARY = java.util.UUID.randomUUID().toString(); //利用系统工具类生成界限符 String PREFIX = "--", LINEND = "\r\n"; String MULTIPART_FROM_DATA = "multipart/form-data"; String CHARSET = "UTF-8"; URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(5 * 1000); // 缓存的最长时间 conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); InputStream in = null; // 发送文件数据 if (files != null) { for (Map.Entry<String, File> file : files.entrySet()) { StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); // name是post中传参的键 filename是文件的名称 sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(file.getValue().getName()).append("\"").append(LINEND); sb1.append("Content-Type: application/octet-stream; charset=").append(CHARSET).append(LINEND); sb1.append("Content-Transfer-Encoding: binary").append(LINEND); sb1.append(LINEND); outStream.write(sb1.toString().getBytes()); InputStream is = new FileInputStream(file.getValue()); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } is.close(); outStream.write(LINEND.getBytes()); } // 请求结束标志 byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); // 得到响应码 int res = conn.getResponseCode(); if (res == 200) { Log.d(TAG,"状态码:"+res); in = conn.getInputStream(); ByteArrayOutputStream bos=new ByteArrayOutputStream(); int l=-1; byte[]buff=new byte[1024]; while ((l=in.read(buff))!=-1) bos.write(buff,0,l); bos.close(); in.close(); outStream.close(); conn.disconnect(); return new String(bos.toByteArray()); }else{ Log.d(TAG,"状态码:"+res); outStream.close(); conn.disconnect(); } } return ""; } }