XMLDecoder和 XMLEncoder 的样例

来源:互联网 发布:金银交易软件 编辑:程序博客网 时间:2024/05/22 06:25
   1public static List<OnlineForum> getList(String param) {  
   
2.   try {  
   
3.     URL url = new URL(param.trim());  
   
4.     HttpURLConnection con = (HttpURLConnection) url.openConnection();  
   
5.     con.setRequestMethod("GET");  
   
6.     InputStream is = con.getInputStream();  
   
7.     XMLDecoder decoder = new XMLDecoder(is);  
   
8.     List<OnlineForum> rtn = (List<OnlineForum>) decoder.readObject();  
   
9.     is.close();  
  
10.     decoder.close();  
  
11.     return rtn;  
  
12.   }
 catch (Exception ex) {  
  
13.     ex.printStackTrace();  
  
14.     return null;  
  
15.   }
  
  
16. }
  
  
17.   
  
18public static boolean post(String param, OnlineForum of) {  
  
19.   try {  
  
20.     URL url = new URL(param.trim());  
  
21.     HttpURLConnection con = (HttpURLConnection) url.openConnection();  
  
22.     con.setRequestMethod("POST");  
  
23.     con.setDoOutput(true);  
  
24.     XMLEncoder encoder = new XMLEncoder(con.getOutputStream());  
  
25.     encoder.writeObject(of);  
  
26.     encoder.close();  
  
27.     InputStream is = con.getInputStream();  
  
28.     byte[] bs = new byte[1024];  
  
29.     int len;  
  
30.     while ((len = is.read(bs)) > 0{  
  
31.       System.out.print(new String(bs, 1, len));  
  
32.     }
  
  
33.     System.out.println("POST OK");  
  
34.     return true;  
  
35.   }
 catch (Exception ex) {  
  
36.     ex.printStackTrace();  
  
37.     return false;  
  
38.   }
  
  
39. }
  
 
原创粉丝点击