一些常用的代码块。后期会慢慢更新
来源:互联网 发布:php过滤标签中的style 编辑:程序博客网 时间:2024/05/04 05:13
<span style="font-size:18px;">/* 1. * 从流中解析新闻集合 * 使用pull解析器解析xml数据*/private static List<NewInfo> getNewListFromInputStream(InputStream is) throws Exception {XmlPullParser parser = Xml.newPullParser();parser.setInput(is, "utf-8");int eventType = parser.getEventType();List<NewInfo> newInfoList = null;NewInfo newInfo = null;while(eventType != XmlPullParser.END_DOCUMENT ){String tagNme = parser.getName();//节点名称switch (eventType) {case XmlPullParser.START_TAG://<news>if("news".endsWith(tagNme)){newInfoList = new ArrayList<NewInfo>();}else if ("new".equals(tagNme)){newInfo = new NewInfo();}else if ("title".equals(tagNme)) {newInfo.setTitle(parser.nextText());}else if ("detail".equals(tagNme)) {newInfo.setDetail(parser.nextText());}else if ("comment".equals(tagNme)) {newInfo.setComment(Integer.valueOf(parser.nextText()));}else if ("image".equals(tagNme)) {newInfo.setImageUrl(parser.nextText());}break;case XmlPullParser.END_TAG:if("new".equals(tagNme)){newInfoList.add(newInfo);}break;default:break;}eventType = parser.next();}return newInfoList; } 2.从输入流中读取数据(可转换为字符串)// 将流转换成字符串private static String getStringInputStream(InputStream is)throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = -1;while ((len = is.read(buffer)) != -1) {bos.write(buffer, 0, len);}/*is.close();return bos.toByteArray(); //从流中读出数据返回出去*/is.close();// String html = bos.toString();//把流中数据转换成字符串 采用的是UTF-8String html = new String(bos.toByteArray(), "GBK");// 在客户端更改,接收GBK编码格式,转换成字符串bos.close();return html;} (读一串字符串,一串!用字符流比较方便。。字节流向字符流的转换。FileInputStream fis = new FileInputStream(path)1.先定义一个字符流对象。BufferedReader reader = new BufferedReader();()里面接收的是一个reader的对象,就是那个转换流BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 3.//根据路径得到image图片(Bitmap) 加载网络图片 public static Bitmap getImage(String path){ HttpURLConnection conn = null; Bitmap bitmap = null; try {URL url = new URL(path);HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();httpconn.connect();InputStream is =httpconn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();httpconn.disconnect();} catch (Exception e) {e.printStackTrace();} return bitmap; } ------------------------------------------------------------------------protected void saveBitmapToSD(Bitmap bt) {String path = Environment.getExternalStorageDirectory();File file = new File(path, System.currentTimeMillis()+".jpg");System.out.println(Environment.getExternalStorageState()+"/Cool/"+"000000000000000000000000000");try {out = new FileOutputStream(file);bt.compress(Bitmap.CompressFormat.JPEG, 90, out);} catch (FileNotFoundException e) {e.printStackTrace();}try {out.flush();out.close();} catch (IOException e) {e.printStackTrace();}} </span>
0 0
- 一些常用的代码块。后期会慢慢更新
- 关于c语言代码的规范(后期会更新)
- 一些常用的JS正则表达式--会慢慢添加
- 常用的一些代码块
- ios接入微信SDK的一些坑(后期会陆续更新)
- ios接入微博SDK的一些坑(后期会陆续更新)
- 一些常用的集合工具的代码块(缓慢更新XD)
- JS中一些常用的代码块
- JS中一些常用的代码块
- JS中一些常用的代码块
- 【收集】常用的一些代码块<1>
- 一些基本的概念(慢慢更新)
- 常用的adb指令(慢慢更新)
- android 一些常用的功能方法代码块
- android 一些常用的功能方法代码块(二)
- android 一些常用的功能方法代码块(二)
- android 一些常用的功能方法代码块 (一)
- android 一些常用的功能方法代码块
- STL vector用法介绍
- 浅析Windows的访问权限检查机制
- 安卓属性动画(Property Animation)(上)
- GNU/Linux 体系结构
- Spring 的优秀工具类盘点,第 1 部分: 文件资源操作和 Web 相关工具类
- 一些常用的代码块。后期会慢慢更新
- 极客学院的Android-async-http的用法
- FastMM、FastCode、FastMove的使用
- Huawei 2015 机试题
- Android 使用动态加载框架DL进行插件化开发
- bzoj 4245: [ONTAK2015]OR-XOR 递推
- msql命令大全
- (1)java自带软件包javax.crypto的使用方法,对称加密方法和非对称加密方法
- C/C++宏定义的可变参数