版本更新2
2016-07-27 15:40
363 查看
//网络 package test.bwie.com.gaoxuge20160701.utils; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * Created by gateway on 2016/7/1. */ public class NetWorkUtils { public static String getString(String path) { try { //创建URL对象 URL url = new URL(path); //获取HttpURLConnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置连接超时时间 conn.setConnectTimeout(5 * 1000); //设置读取超时时间 conn.setReadTimeout(5000); //请求方式 conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if (code == 200) { //从流中读取数据 InputStream inStream = conn.getInputStream(); int len = -1; byte[] arr = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((len = inStream.read(arr)) != -1) { builder.append(new String(arr, 0, len)); } return builder.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; } public static InputStream getInputstreams(String path) { try { //穿件url URL url = new URL(path); //获取HttpURLConnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置连接超时时间 conn.setConnectTimeout(5 * 1000); //读取超时时间 conn.setReadTimeout(5000); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if (code == 200) { //获取流 InputStream inStream = conn.getInputStream(); //返回 return inStream; } } catch (Exception e) { e.printStackTrace(); } return null; } }
相关文章推荐
- Android PendingIntent的理解
- Android PendingIntent的理解
- Android PendingIntent的理解
- Android PendingIntent的理解
- js简单校验
- linux下的cron服务详解
- 使用Maven构建web app开发项目,并配置tomcat
- RxJava两步打造华丽的Android引导页
- MySql链接错误提示:Unknown character set: 'utf8mb4'
- Netty源码解读(二)Netty中的buffer
- 《疯狂JAVA讲义》缓存实例的不可变类
- 自定义 URL Scheme 完全指南(转载)
- javaWeb项目中到底什么是单例,多例
- 【Android】使用Gradle打包时,获取svn的版本号,删除unalign.apk文件,获得版本号,并设定在打包的文件名称中
- 3段代码搞定继承--3个代码就够了
- CSS图片垂直居中方法
- mac开启safari开发模式(用于OC和JS交互)
- html5中section元素的使用方法
- 【通讯术语】AAA
- 版本更新