目前在51,校内,facebook开发小应用越来越热,这里我仅以xiaonei.com提供的APP Java API为例,探讨他的实现方法,简单来说就是分为2步:
- 申请个应用,得到应用的API Key和Secret两个数值
- 利用这两个数值,我们就可以使用xiaonei的Java API,以下代码用来实现获取你的个人信息和你的好友信息代码(由于邀请API已过期,所以不能使用):
/** *//**
* http://www.bt285.cn http://www.5a520.cn
*/
package com.cxlh.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.Document;
import com.common.utils.StrCharUtil;
import com.xiaonei.api.ProfileField;
import com.xiaonei.api.XiaoneiException;
import com.xiaonei.api.XiaoneiRestClient;
import com.xiaonei.api.schema.Friend;
import com.xiaonei.api.schema.FriendsGetFriendsResponse;
import com.xiaonei.api.schema.RequestsSendRequestResponse;
import com.xiaonei.api.schema.UsersGetInfoResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/** *//**
* Servlet implementation class XN
*/
public class XN extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String PARAM_XN_SIG_IN_IFRAME = "xn_sig_in_iframe";
public static String PARAM_XN_SIG_TIME = "xn_sig_time";
public static String PARAM_XN_SIG_ADDED = "xn_sig_added";
public static String PARAM_XN_SIG_USER = "xn_sig_user";
public static String PARAM_XN_SIG_SESSION_KEY = "xn_sig_session_key";
public static String PARAM_XN_SIG_API_KEY = "xn_sig_api_key";
/** *//**
* @see HttpServlet#HttpServlet()
*/
public XN() {
super();
// TODO Auto-generated constructor stub
}
private List<Friend> getFriend(XiaoneiRestClient client) throws Exception{
client.friends_getFriends();
FriendsGetFriendsResponse resp = (FriendsGetFriendsResponse) client.getResponsePOJO();
List<Friend> friends = resp.getFriend();
return friends;
}
}
private String buildUserInfo(int id) {
StringBuffer sb=new StringBuffer();
sb.append("<div align=\"center\">\n");
sb.append("图片:http://www.5a520.cn <xn:profile-pic uid=\"");
sb.append(id);
sb.append("\" linked=\"true\" size=\"tiny\"/><br>\n");
sb.append("姓名:<xn:name uid=\""+id+"\" linked=\"true\" shownetwork=\"true\" />\n");
sb.append("</div>\n");
return sb.toString();
}
/** *//**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Charset", "utf-8");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("UTF-8");
JSONObject json = new JSONObject();
String cmd=StrCharUtil.formatNullStr(request.getParameter("cmd"));
String callback=StrCharUtil.formatNullStr(request.getParameter("callback"));
String sessionKey = request.getParameter(PARAM_XN_SIG_SESSION_KEY);
XiaoneiRestClient client = new XiaoneiRestClient("你的APIKey",
"你的Secret Key", sessionKey);
//client.setDebug(true);
try{
if("getfriends".equals(cmd)){
JSONArray jj = JSONArray.fromObject(getFriend(client));
json.put("res",jj.toString());
}else if("myinfor".equals(cmd)){
int loggedInUserId = 0;
try {
loggedInUserId = client.users_getLoggedInUser();
EnumSet<ProfileField> enumFields = EnumSet.of(ProfileField.NAME,
ProfileField.HOMETOWN_LOCATION,ProfileField.BIRTHDAY,
ProfileField.HEADURL,ProfileField.MAINURL,
ProfileField.SEX,ProfileField.STAR,ProfileField.TINYURL,
ProfileField.ZIDOU,ProfileField.UNIVERSITY_HISTORY,
ProfileField.WORK_HISTORY);
Document doc = client.users_getInfo(loggedInUserId, enumFields);
json.put("xml",doc.toString());
UsersGetInfoResponse loggedUserGetInfoRes = (UsersGetInfoResponse) client.getResponsePOJO();
json.put("res",JSONArray.fromObject(loggedUserGetInfoRes.getUser()));
}
catch(XiaoneiException e) {
e.printStackTrace();
}
}else if("invite".equals(cmd)){
List<Integer> userIds = new ArrayList();
List<Friend> friends = getFriend(client);
for (Friend f:friends) {
userIds.add(f.getId());
}
client.requests_sendRequest(userIds);
request.setAttribute("uids", client.getRawResponse());
//进一步看看都成功邀请了哪些朋友 http://www.feng123.com
RequestsSendRequestResponse resp = (RequestsSendRequestResponse) client.getResponsePOJO();
List<Integer> uids = resp.getUid();
json.put("res",JSONArray.fromObject(uids));
}
if(json!=null){
if(callback.equals("")){ //没有回调函数
response.getWriter().println(json.toString());
}else{
response.setContentType("text/javascript;charset=utf-8");
response.getWriter().println(callback+"("+json.toString()+")");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
/** *//**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(request, response);
}
}