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("http://api.xiaonei.com/restserver.do");
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;
}
}