Pencil.Java

          to dig Java
          數據加載中……
          Jdom+HttpClient寫的校內網APP平臺用戶信息獲取類

          校內官方提供的不會用。沒說明,也懶得看代碼。自己寫了一個基本信息獲取class。功能有限,主要是為了這幾天要開發的一個APP所用。

           

          package xiaonei;
          import java.io.IOException;
          import java.io.StringReader;
          import java.util.List;

          import org.apache.commons.httpclient.HttpClient;
          import org.apache.commons.httpclient.HttpException;
          import org.apache.commons.httpclient.NameValuePair;
          import org.apache.commons.httpclient.methods.PostMethod;
          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.JDOMException;
          import org.jdom.Namespace;
          import org.jdom.input.SAXBuilder;
          import org.xml.sax.InputSource;

           /**
           * 調用校內Web服務的客戶端
           * @author Pencil 

           * @QQ 552526

           */


          //生成客戶端
          public class Client{
           private String apikey = "";
           private String sessionkey = "";
           public Client(String apikey,String sessionkey){
            this.apikey = apikey;
            this.sessionkey = sessionkey;
            
           }
           
              public String getStream(String uid,String field) throws HttpException, IOException, JDOMException{
               HttpClient client = new HttpClient();
               PostMethod post = new UTF8PostMethod("

                  NameValuePair[] fixedParam = {
                          new NameValuePair("method", "xiaonei.users.getInfo"),
                          new NameValuePair("v", "1.0"),
                          new NameValuePair("api_key", apikey),
                          new NameValuePair("session_key", sessionkey),
                          new NameValuePair("fields",field ),
                          new NameValuePair("uids", uid)
                      };
                  post.addParameters(fixedParam);
                  client.executeMethod(post);
                 
               String s = post.getResponseBodyAsString();
                  StringReader read = new StringReader(s);
                  //創建新的輸入源SAX 解析器將使用 InputSource 對象來確定如何讀取 XML 輸入
                  InputSource source = new InputSource(read);
               SAXBuilder builder = new SAXBuilder();

            Document doc = builder.build(source);
            Element foo = doc.getRootElement(); 
               Namespace ns = foo.getNamespace();
            List allChildren = foo.getChildren();
            String info = ((Element)allChildren.get(0)).getChild(field,ns).getText();
                  if(field.equals("province")){
                   String province = ((Element)allChildren.get(0)).getChild("hometown_location",ns).getChild("province",ns).getText();
                      return province;
                  }
                  if(field.equals("city")){
                   String city = ((Element)allChildren.get(0)).getChild("hometown_location",ns).getChild("city",ns).getText();
                      return city;
                  }
                  return info;
           
           }
              //編碼轉換
              public static class UTF8PostMethod extends PostMethod {

                  public UTF8PostMethod(String url) {
                      super(url);
                  }

                  @Override
                  public String getRequestCharSet() {
                      //return super.getRequestCharSet();
                      return "UTF-8";
                  }
              }
              //獲取用戶姓名
              public String getName(String uid) throws HttpException, IOException, JDOMException{
               String name = this.getStream(uid, "name");
            return name;
               
              }
              //獲取用戶性別 true是男
              public boolean getSex(String uid) throws HttpException, IOException, JDOMException{
               String sex = this.getStream(uid, "sex");
               if(sex.equals("1"))
                return true;
               else
                return false;
               
              }
              //獲取用戶生日  格式:yyyy-MM-dd
              public String getBirthday(String uid) throws HttpException, IOException, JDOMException{
               String birthday = this.getStream(uid, "birthday");
               return birthday;
               
              }
              //判斷用戶是否為星級用戶  true 為是
              public boolean isStar(String uid) throws HttpException, IOException, JDOMException{
               String star = this.getStream(uid, "star");
               if(star.equals("1"))
                return true;
               else
                return false;
              }
              //獲取用戶小圖url  
              public String getTinyurl(String uid) throws HttpException, IOException, JDOMException{
               String tinyurl = this.getStream(uid, "tinyurl");
               return tinyurl;
              }
              //獲取用戶標準頭像圖片url
              public String getHeadurl(String uid) throws HttpException, IOException, JDOMException{
               String headurl = this.getStream(uid, "headurl");
               
            return headurl;
               
              }
              //獲取用戶家鄉所 省
              public String getProvince(String uid) throws HttpException, IOException, JDOMException{
               String province = this.getStream(uid, "province");
            return province; 
              }
              //獲取用戶家鄉所在城市
              public String getCity(String uid) throws HttpException, IOException, JDOMException{
               String city = this.getStream(uid, "city");
               return city;     
              }
             

          }

          posted on 2009-04-17 23:33 鉛筆 閱讀(661) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 太仓市| 彭州市| 工布江达县| 肥东县| 宁河县| 博罗县| 柳河县| 汾西县| 淮北市| 乐山市| 乌什县| 德兴市| 驻马店市| 玉林市| 吉首市| 内乡县| 娄底市| 上思县| 江门市| 南丹县| 黄冈市| 龙井市| 涞源县| 和静县| 饶平县| 盐城市| 新营市| 海原县| 新余市| 平泉县| 垫江县| 武安市| 沁阳市| 永平县| 双城市| 琼海市| 弥渡县| 东乡| 临漳县| 固阳县| 营口市|