简单的获取网络数据 测试使用
2017-01-04 16:38
555 查看
直接上代码
public void getHttpData(String strURL) { HttpURLConnection connection = null; try { URL url = new URL(strURL); connection = (HttpURLConnection) url.openConnection(); // 设置请求方法,默认是GET connection.setRequestMethod("GET"); // 设置字符集 connection.setRequestProperty("Charset", "UTF-8"); // 设置文件类型 connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); // 设置请求参数,可通过Servlet的getHeader()获取 connection.setRequestProperty("Cookie", "AppName=" + URLEncoder.encode("你好", "UTF-8")); // 设置自定义参数 connection.setRequestProperty("MyProperty", "this is me!"); if (connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); } String strMyResult = byteArrayOutputStream.toString(); Log.i("after","测试数据"+strMyResult); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } }
new Thread(new Runnable() { @Override public void run() { getHttpData("http://www.cnblogs.com/bokeyuan007/p/5550883.html"); } }).start();
相关文章推荐
- Android建立网络连接,利用JSON数据获取百度图片搜索结果及GSON的简单使用
- 在vb中使用Iphlpapi.dll获取网络信息 第四章 第三节 实例一:网络数据流量图
- 简单使用ODBC测试数据源
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- 使用 HttpURLConnection 获取不到网络数据
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
- winsock简单使用(采用select轮询方式,从客户端获取数据)
- [置顶] 获取网络数据中的数组显示成ListView的简单流程
- 通过网络请求获取数据时,如果立即使用就会报错:数据为空
- 使用matlab工具研究神经网络的简单过程(网络和数据下载)
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- 不直接使用NSData的dataWithContentsOfURL来获取网络数据
- 使用GDAL获取网络数据
- PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
- 最简单的使用程序进行网络链接测试
- 简单使用ADO测试数据源
- JSP基础语法之一:Scriptlet使用、简单的JSP获取表单数据再输出表格
- proc文件系统中cpu,内存,网络数据使用情况获取(附检测网速源码)
- 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)