本帖最后由 yanghe123 于 2012-5-29 15:03 编辑

在网上看到的一个不错的小例子,贴出来大家一起看看。

代码片段,双击复制
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import android.util.Log; public class JSON { /** * 获取"数组形式"的JSON数据, * 数据形式:[{"id":1,"name":"小猪"},{"id":2,"name":"小猫"}] * @param path 网页路径 * @return 返回List * @throws Exception */ public static List<Map<String, String>> getJSONArray(String path) throws Exception { String json = null ; List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map = null ; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 利用HttpURLConnection对象,我们可以从网络中获取网页数据. conn.setConnectTimeout( 5 * 1000 ); // 单位是毫秒,设置超时时间为5秒 conn.setRequestMethod( "GET" ); // HttpURLConnection是通过HTTP协议请求path路径的,所以需要设置请求方式,可以不设置,因为默认为GET if (conn.getResponseCode() == 200 ) { // 判断请求码是否是200码,否则失败 InputStream is = conn.getInputStream(); // 获取输入流 byte [] data = readStream(is); // 把输入流转换成字符数组 json = new String(data); // 把字符数组转换成字符串 //数据形式:[{"id":1,"name":"小猪","age":22},{"id":2,"name":"小猫","age":23}] JSONArray jsonArray = new JSONArray(json); //数据直接为一个数组形式,所以可以直接 用android提供的框架JSONArray读取JSON数据,转换成Array for ( int i = 0 ; i < jsonArray.length(); i++) { JSONObject item = jsonArray.getJSONObject(i); //每条记录又由几个Object对象组成 int id = item.getInt( "id" ); // 获取对象对应的值 String name = item.getString( "name" ); map = new HashMap<String, String>(); // 存放到MAP里面 map.put( "id" , id + "" ); map.put( "name" , name); list.add(map); } } // ***********测试数据****************** for (Map<String, String> list2 : list) { String id = list2.get( "id" ); String name = list2.get( "name" ); Log.i( "abc" , "id:" + id + " | name:" + name); } return list; } /** * 获取"对象形式"的JSON数据, * 数据形式:{"total":2,"success":true,"arrayData":[{"id":1,"name":"小猪"},{"id":2,"name":"小猫"}]} * @param path 网页路径 * @return 返回List * @throws Exception */ public static List<Map<String, String>> getJSONObject(String path) throws Exception { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map = null ; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 利用HttpURLConnection对象,我们可以从网络中获取网页数据. conn.setConnectTimeout( 5 * 1000 ); // 单位是毫秒,设置超时时间为5秒 conn.setRequestMethod( "GET" ); // HttpURLConnection是通过HTTP协议请求path路径的,所以需要设置请求方式,可以不设置,因为默认为GET if (conn.getResponseCode() == 200 ) { // 判断请求码是否是200码,否则失败 InputStream is = conn.getInputStream(); // 获取输入流 byte [] data = readStream(is); // 把输入流转换成字符数组 String json = new String(data); // 把字符数组转换成字符串 //数据形式:{"total":2,"success":true,"arrayData":[{"id":1,"name":"小猪"},{"id":2,"name":"小猫"}]} JSONObject jsonObject= new JSONObject(json); //返回的数据形式是一个Object类型,所以可以直接转换成一个Object int total=jsonObject.getInt( "total" ); Boolean success=jsonObject.getBoolean( "success" ); Log.i( "abc" , "total:" + total + " | success:" + success); //测试数据 JSONArray jsonArray = jsonObject.getJSONArray( "arrayData" ); //里面有一个数组数据,可以用getJSONArray获取数组 for ( int i = 0 ; i < jsonArray.length(); i++) { JSONObject item = jsonArray.getJSONObject(i); // 得到每个对象 int id = item.getInt( "id" ); // 获取对象对应的值 String name = item.getString( "name" ); map = new HashMap<String, String>(); // 存放到MAP里面 map.put( "id" , id + "" ); map.put( "name" , name); list.add(map); } } // ***********测试数据****************** for (Map<String, String> list2 : list) { String id = list2.get( "id" ); String name = list2.get( "name" ); Log.i( "abc" , "id:" + id + " | name:" + name); } return list; } /** * 获取类型复杂的JSON数据 *数据形式: {"name":"小猪", "age":23, "content":{"questionsTotal":2, "questions": [ { "question": "what's your name?", "answer": "小猪"},{"question": "what's your age", "answer": "23"}] } } * @param path 网页路径 * @return 返回List * @throws Exception */ public static List<Map<String, String>> getJSON(String path) throws Exception { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map = null ; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 利用HttpURLConnection对象,我们可以从网络中获取网页数据. conn.setConnectTimeout( 5 * 1000 ); // 单位是毫秒,设置超时时间为5秒 conn.setRequestMethod( "GET" ); // HttpURLConnection是通过HTTP协议请求path路径的,所以需要设置请求方式,可以不设置,因为默认为GET if (conn.getResponseCode() == 200 ) { // 判断请求码是否是200码,否则失败 InputStream is = conn.getInputStream(); // 获取输入流 byte [] data = readStream(is); // 把输入流转换成字符数组 String json = new String(data); // 把字符数组转换成字符串 /*数据形式: {"name":"小猪", "age":23, "content":{"questionsTotal":2, "questions": [ { "question": "what's your name?", "answer": "小猪"},{"question": "what's your age", "answer": "23"}] } } */ JSONObject jsonObject=new JSONObject(json); //返回的数据形式是一个Object类型,所以可以直接转换成一个Object String name=jsonObject.getString("name"); int age=jsonObject.getInt("age"); Log.i("abc", "name:" + name + " | age:" + age); //测试数据 JSONObject contentObject=jsonObject.getJSONObject("content"); //获取对象中的对象 String questionsTotal=contentObject.getString("questionsTotal"); //获取对象中的一个值 Log.i("abc", "questionsTotal:" + questionsTotal); //测试数据 JSONArray contentArray=contentObject.getJSONArray("questions"); //获取对象中的数组 for (int i = 0; i < contentArray.length(); i++) { JSONObject item = contentArray.getJSONObject(i); // 得到每个对象 String question = item.getString("question"); // 获取对象对应的值 String answer = item.getString("answer"); map = new HashMap<String, String>(); // 存放到MAP里面 map.put("question", question); map.put("answer", answer); list.add(map); } } // ***********测试数据****************** for (Map<String, String> list2 : list) { String question = list2.get("question"); String answer = list2.get("answer"); Log.i("abc", "question:" + question + " | answer:" + answer); } return list; } /** * 把输入流转换成字符数组 * @param inputStream 输入流 * @return 字符数组 * @throws Exception */ public static byte [] readStream(InputStream inputStream) throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte [] buffer = new byte [ 1024 ]; int len = 0 ; while ((len = inputStream.read(buffer)) != - 1 ) { bout.write(buffer, 0 , len); } bout.close(); inputStream.close(); return bout.toByteArray(); } }

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. 箭头函数的基础使用
  3. python起点网月票榜字体反爬案例
  4. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  5. Android屏幕分辨率正确获取及PX,DPI,DP,SP等的对应关系
  6. 《Android开发从零开始》——25.数据存储(4)
  7. Android系统配置数据库注释(settings.db)
  8. Android中不同应用间实现SharedPreferences数据共享
  9. android 获取唯一标识

随机推荐

  1. SpaceX成功海上回收火箭(附视频):从星辰到
  2. Python与苹果哲学的相似之处
  3. Tomcat就是这么简单(修订版)
  4. 聊聊Docker镜像
  5. FBI是如何攻破iPhone的?
  6. 快速尝试Python模块:try
  7. 微软Edge浏览器准备内置屏蔽广告功能
  8. 嗯,我来杭州了。
  9. 数据库两个神器索引和锁(修订版)
  10. 一个70后“大叔”的PMP学习考证之路