XMLDecoder和 XMLEncoder 的样例


  1. public static List  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  rtn = (List ) decoder.readObject();  
  9.     is.close();  
  10.     decoder.close();  
  11.     return rtn;  
  12.   } catch (Exception ex) {  
  13.     ex.printStackTrace();  
  14.     return null;  
  15.   }  
  16. }  
  17.   
  18. public 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.   }  


本文转自

http://www.java2000.net/viewthread.jsp?tid=2497

你可能感兴趣的:(XMLDecoder和 XMLEncoder 的样例)