JAVA_Gson_example
1 package cn.kjxy.GSON; 2 3 import java.util.List; 4 5 import cn.kjxy.JSON.HttpHelpers; 6 7 import com.google.gson.Gson; 8 9 /** 10 * gson解析json对象 : 11 * 注意:1.建立的属性或者对象名必须与json数据中的一致才能解析出来 12 * 2。需导入gson-2.2.1.jar包 13 * 工具类需导入:commons-logging-1.1.1.jar 14 * httpclient-4.1.2.jar 15 * httpcore-4.1.2.jar 16 * 17 * @author Administrator 18 * 19 */ 20 class Food { 21 private int ret; 22 private List<Data> data; 23 24 @Override 25 public String toString() { 26 return "Food [ret=" + ret + ", data=" + data + "]"; 27 } 28 29 } 30 31 class Data { 32 33 private int id;// ": "8289", 34 private String title;// ": "油焖大虾", 35 private String pic;// ": "http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg", 36 private int collect_num;// ": "1544", 37 private String food_str;// ": "大虾 葱 生姜 植物油 料酒", 38 private int num;// ": 1544 39 40 @Override 41 public String toString() { 42 return "Data [id=" + id + ", title=" + title + ", pic=" + pic 43 + ", collect_num=" + collect_num + ", food_str=" + food_str 44 + ", num=" + num + "]"; 45 } 46 47 } 48 49 public class Demo2 { 50 public static void main(String[] args) { 51 // gson解析json对象 52 String json1 = HttpHelpers 53 .getResourceByInternet("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&page=1&limit=20"); 54 parserJsonByGson(json1); 55 } 56 57 private static void parserJsonByGson(String json1) { 58 // TODO Auto-generated method stub 59 Gson gson = new Gson(); 60 Food food = gson.fromJson(json1, Food.class); 61 System.out.println(food); 62 } 63 64 }
工具类:
1 package cn.kjxy.JSON; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.BufferedReader; 6 import java.io.ByteArrayOutputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.Map; 14 15 import org.apache.http.HttpEntity; 16 import org.apache.http.HttpResponse; 17 import org.apache.http.NameValuePair; 18 import org.apache.http.client.HttpClient; 19 import org.apache.http.client.entity.UrlEncodedFormEntity; 20 import org.apache.http.client.methods.HttpGet; 21 import org.apache.http.client.methods.HttpPost; 22 import org.apache.http.impl.client.DefaultHttpClient; 23 import org.apache.http.message.BasicNameValuePair; 24 import org.apache.http.util.EntityUtils; 25 26 /** 27 * 请求网络的工具类 28 * 29 * @author Administrator 30 * 31 */ 32 public class HttpHelpers { 33 34 /** 35 * 下载图片 保存到byte类型的数组中 36 * 37 * @param path 38 * 地址 39 * @return byte[] 40 */ 41 public static byte[] downLoadImg(String path) { 42 BufferedInputStream bis = null; 43 try { 44 // 1,创建HttpClient对象 Android6.0之前可以使用 45 HttpClient httpClient = new DefaultHttpClient(); 46 // 2,创建请求对象+指定地址 47 HttpGet httpGet = new HttpGet(path); 48 // 3,执行请求 获得HttpResponse对象 49 HttpResponse response = httpClient.execute(httpGet); 50 // 4,获得响应码 51 int code = response.getStatusLine().getStatusCode(); 52 if (code == 200) { 53 // 5,得到响应的HttpEntity对象 54 HttpEntity responseEntity = response.getEntity(); 55 // 方法一 56 // bis = new BufferedInputStream(responseEntity.getContent()); 57 // byte b[] = toByteArray(bis); 58 // return b; 59 60 // 方法二 61 return EntityUtils.toByteArray(responseEntity); 62 63 } 64 65 } catch (IOException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } finally { 69 if (bis != null) { 70 try { 71 bis.close(); 72 } catch (IOException e) { 73 // TODO Auto-generated catch block 74 e.printStackTrace(); 75 } 76 } 77 } 78 79 return null; 80 81 } 82 83 /** 84 * 把图片下载到本地磁盘 85 * 86 * @param path 87 */ 88 public static void downLoadImgToLocal(String path) { 89 BufferedInputStream bis = null; 90 BufferedOutputStream boStream = null; 91 try { 92 // 1,创建HttpClient对象 Android6.0之前可以使用 93 HttpClient httpClient = new DefaultHttpClient(); 94 // 2,创建请求对象+指定地址 95 HttpGet httpGet = new HttpGet(path); 96 // 3,执行请求 获得HttpResponse对象 97 HttpResponse response = httpClient.execute(httpGet); 98 // 4,获得响应码 99 int code = response.getStatusLine().getStatusCode(); 100 if (code == 200) { 101 // 5,得到响应的HttpEntity对象 102 HttpEntity responseEntity = response.getEntity(); 103 // 方法一 104 // bis = new BufferedInputStream(responseEntity.getContent()); 105 // byte b[] = toByteArray(bis); 106 107 // 方法二 108 byte b[] = EntityUtils.toByteArray(responseEntity); 109 String endsWith = path.substring(path.lastIndexOf(".")); 110 boStream = new BufferedOutputStream(new FileOutputStream( 111 (int) (Math.random() * 100) + endsWith)); 112 boStream.write(b); 113 114 } 115 116 } catch (IOException e) { 117 // TODO Auto-generated catch block 118 e.printStackTrace(); 119 } finally { 120 if (bis != null) { 121 try { 122 bis.close(); 123 } catch (IOException e) { 124 // TODO Auto-generated catch block 125 e.printStackTrace(); 126 } 127 } 128 if (boStream != null) { 129 try { 130 boStream.close(); 131 } catch (IOException e) { 132 // TODO Auto-generated catch block 133 e.printStackTrace(); 134 } 135 } 136 } 137 138 } 139 140 /** 141 * 从互联网获取文本 json xml 142 * 143 * @param path 144 * 地址 145 * @return 获取到的文本数据 146 */ 147 148 public static String getResourceByInternet(String path) { 149 BufferedReader bReader = null; 150 try { 151 // 1,创建HttpClient对象 Android6.0之前可以使用 152 HttpClient httpClient = new DefaultHttpClient(); 153 // 2,创建请求对象+指定地址 154 HttpGet httpGet = new HttpGet(path); 155 // 3,执行请求 获得HttpResponse对象 156 HttpResponse response = httpClient.execute(httpGet); 157 // 4,获得响应码 158 int code = response.getStatusLine().getStatusCode(); 159 if (code == 200) { 160 // 得到HttpEntity对象 161 HttpEntity responseEntity = response.getEntity(); 162 // 方法一 163 // bReader = new BufferedReader(new 164 // InputStreamReader(responseEntity.getContent())); 165 // StringBuilder sbBuilder = new StringBuilder(); 166 // String line = null; 167 // while ((line = bReader.readLine()) != null) { 168 // sbBuilder.append(line); 169 // } 170 // 171 // return sbBuilder.toString(); 172 173 // 方法二 174 return EntityUtils.toString(responseEntity); 175 176 } 177 178 } catch (IOException e) { 179 // TODO Auto-generated catch block 180 e.printStackTrace(); 181 } finally { 182 if (bReader != null) { 183 try { 184 bReader.close(); 185 } catch (IOException e) { 186 // TODO Auto-generated catch block 187 e.printStackTrace(); 188 } 189 } 190 } 191 192 return null; 193 194 } 195 196 public static byte[] toByteArray(BufferedInputStream bufferedInputStream) { 197 byte b[] = new byte[1024 * 1024]; 198 int len = 0; 199 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 200 try { 201 while ((len = bufferedInputStream.read(b)) != -1) { 202 baos.write(b, 0, len); 203 } 204 } catch (IOException e) { 205 // TODO Auto-generated catch block 206 e.printStackTrace(); 207 } 208 209 return baos.toByteArray(); 210 211 } 212 213 public static String upLoadData(String path, Map<String, String> map) { 214 BufferedReader bReader = null; 215 try { 216 // 1,创建HttpClient对象 Android6.0之前可以使用 217 HttpClient httpClient = new DefaultHttpClient(); 218 // 2,创建请求对象+指定地址 219 HttpPost httpPost = new HttpPost(path); 220 // 设置用于发送到服务端的参数 221 List<NameValuePair> list = new ArrayList<NameValuePair>(); 222 223 for (String string : map.keySet()) { 224 list.add(new BasicNameValuePair(string, map.get(string))); 225 } 226 HttpEntity requestEntity = new UrlEncodedFormEntity(list, "gbk"); 227 httpPost.setEntity(requestEntity); 228 229 // 3,执行请求 获得HttpResponse对象 230 HttpResponse response = httpClient.execute(httpPost); 231 // 4,获得响应码 232 int code = response.getStatusLine().getStatusCode(); 233 if (code == 200) { 234 // 得到HttpEntity对象 235 HttpEntity responseEntity = response.getEntity(); 236 // 方法一 237 // bReader = new BufferedReader(new 238 // InputStreamReader(responseEntity.getContent())); 239 // StringBuilder sbBuilder = new StringBuilder(); 240 // String line = null; 241 // while ((line = bReader.readLine()) != null) { 242 // sbBuilder.append(line); 243 // } 244 // 245 // return sbBuilder.toString(); 246 247 // 方法二 248 return EntityUtils.toString(responseEntity, "gbk"); 249 250 } 251 252 } catch (IOException e) { 253 // TODO Auto-generated catch block 254 e.printStackTrace(); 255 } finally { 256 if (bReader != null) { 257 try { 258 bReader.close(); 259 } catch (IOException e) { 260 // TODO Auto-generated catch block 261 e.printStackTrace(); 262 } 263 } 264 } 265 266 return null; 267 268 } 269 270 public static void main(String[] args) { 271 byte b[] = downLoadImg("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg"); 272 BufferedOutputStream bufferedOutputStream; 273 try { 274 bufferedOutputStream = new BufferedOutputStream( 275 new FileOutputStream("ceo.jpg")); 276 // bufferedOutputStream.write(b); 277 278 } catch (FileNotFoundException e1) { 279 // TODO Auto-generated catch block 280 e1.printStackTrace(); 281 } catch (IOException e) { 282 // TODO Auto-generated catch block 283 e.printStackTrace(); 284 } 285 286 // downLoadImgToLocal("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg"); 287 288 // System.out.println(getResourceByInternet("http://www.doukantv.com/api/hot/?type=movie&cli=ipad&sys_ver=7.1.1&ver=2.1")); 289 Map<String, String> map = new HashMap<String, String>(); 290 map.put("uname", "张三"); 291 map.put("pwd", "admin"); 292 System.out.println(upLoadData( 293 "http://172.20.136.5:8080/2016_05_27_server/login", map)); 294 295 } 296 297 }