Android Http网络数据传输备忘
2011-06-18 15:17
267 查看
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; public class HttpTool { private static int TIME_OUT = 1000 * 6; private static String POST = "POST"; private static String GET = "GET"; private static String UTF8 = "UTF-8"; public static void downloadGET(String path) throws Exception{ URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(TIME_OUT); connection.setRequestMethod(GET); connection.setUseCaches(false); connection.connect(); if(connection.getResponseCode() != 200){ throw new RuntimeException("ResponseCode != 200"); } InputStream inputStream = connection.getInputStream(); File file = new File(rename(path)); FileOutputStream fileOutputStream = new FileOutputStream(file); readinput(inputStream, fileOutputStream); connection.disconnect(); System.out.println("ok"); } public static void downloadPOST(String path,Map<String, String> params) throws Exception{ URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(TIME_OUT); connection.setRequestMethod(POST); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestProperty("Accept-Charset", UTF8); connection.setRequestProperty("Connection", "keep-alive"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset="+UTF8); StringBuilder sb = new StringBuilder(); if(params != null){ for(Map.Entry<String, String> item : params.entrySet()){ sb.append(item.getKey()+"="+item.getValue()).append("&"); } sb = sb.deleteCharAt(sb.length()-1); byte[] data = sb.toString().getBytes(); connection.setRequestProperty("Content-Length",String.valueOf(data.length)); OutputStream outputStream = connection.getOutputStream(); outputStream.write(data); outputStream.flush(); outputStream.close(); } if(connection.getResponseCode() != 200){ throw new RuntimeException("ResponseCode != 200"); } InputStream inputStream = connection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); while((len = bufferedInputStream.read(buffer)) != -1){ arrayOutputStream.write(buffer, 0, len); } byte[] bytes = arrayOutputStream.toByteArray(); String str = new String(bytes,UTF8); arrayOutputStream.close(); bufferedInputStream.close(); connection.disconnect(); System.out.println(str); } /** * 读取输入流 * @param inputStream * @param fileOutputStream * @throws Exception */ private static void readinput(InputStream inputStream,FileOutputStream fileOutputStream) throws Exception{ BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); byte[] buffer = new byte[1024]; int len = -1; while((len = bufferedInputStream.read(buffer)) != -1){ bufferedOutputStream.write(buffer, 0, len); } bufferedOutputStream.close(); bufferedInputStream.close(); } /** * 重名名 * @param path * @return */ public static String rename(String path){ String str = path.substring(path.lastIndexOf(".")); return new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date())+str; }
相关文章推荐
- Android网络通信技术(HttpClient),在网络上运用(SerializableEntity)直接传输对象
- android重新学_网络3——采用HttpURLConnection提交数据到服务器
- Android-网络请求数据使用apache httpclient v4
- Android 网络编程之HttpURLConnection(conn.getResponseCode() != 200,从url读取写入数据)
- Android开发案例:网络交互&XML与json数据解析&HttpUtil优化
- Android使用HTTP GET、HTTP POST获取网络数据
- 网络数据传输socket和http优缺点
- android 请求网络数据 HttpURLConnection
- Android利用Json来进行网络数据传输
- Qt实现网络数据传输 HttpClient
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- android网络文字数据的传输
- 【开源中国Android客户端】源码分析 网络数据传输接口
- Android网络数据加密传输
- Android利用Json来进行网络数据传输
- 【Android Advanced Training - 05】传输数据时避免电量的浪费[Lesson 1 - 看无线电波如何影响网络操作]
- Android HttpClient多线程获取网络数据抛出ConnectionPoolTimeoutException: Timeout waiting for connection
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- android 关于网络连接之传输数据节省电量2
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)