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 鉛筆 閱讀(662) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 商水县| 海阳市| 内黄县| 霸州市| 察哈| 宜宾县| 莎车县| 武宁县| 西华县| 什邡市| 资兴市| 应城市| 郯城县| 横峰县| 固安县| 泾阳县| 钦州市| 项城市| 达拉特旗| 文化| 马关县| 美姑县| 大连市| 南宫市| 白银市| 许昌市| 翁牛特旗| 毕节市| 安达市| 陆川县| 法库县| 郓城县| 甘洛县| 石楼县| 宜春市| 垫江县| 北碚区| 察雅县| 澄城县| 什邡市| 淮阳县|