Android判断网络状态并远程获取数据
2016-03-23 13:55
543 查看
public class NetUtility { /** * 获取网络是否连接 * @param context * @return 网络是连通的话返回true,都则返回false。 */ public static boolean isNetworkAvailable(Context context) { try { ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.isConnected()) { if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } catch (Exception e) { } return false; } /** * 从网上获取文本内容 * @param strURL 服务器地址 * @return 服务器端的响应文本内容。出错则返回null。 */ public static String getNetText(String strURL){ try { URL url = new URL(strURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5000); con.connect(); InputStreamReader is = new InputStreamReader(con.getInputStream()); BufferedReader br = new BufferedReader(is); String line; StringBuffer sb = new StringBuffer(); while ((line = br.readLine()) != null) { sb.append(line); } br.close(); is.close(); con.disconnect(); return sb.toString(); }catch (Exception ex) { ex.printStackTrace(); return null; } } }
相关文章推荐
- Httperf:测试服务器能承载的最大服务!!!
- 23. XML 解析 / HTTP 请求方法 / 模拟登录 / MD5
- 22.NSURLConnection & NSRULSession 入门 / 网络请求监视 / JSON解析
- 21.HTTP原理 & Socket开发体验
- ViewPage+Volley实现动态加载网络图片广告轮播 !
- 《Metasploit魔鬼训练营》虚拟环境搭建中网络配置的一些问题
- 实例讲解虚拟机3种网络模式(桥接、nat、Host-only)
- http协议学习
- Linux学习笔记(七)-网络篇和多媒…
- 网络认证
- Tcp三次握手,四次挥手
- TCP协议中的三次握手和四次挥手(图解)
- 案例:演示TCP传输过程的实现
- TCP通信案例分析
- httpClient(Get)进行网络请求
- 利用网络进行文件的上传,通过客户端上传到服务端
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
- HttpClient工具类v1.7
- vmware虚拟机中ubuntu上网问题
- 网络七层协议