Java调用微博实例

1、下载Java实例源码 http://weibo4j.googlecode.com/files/weibo4j-1.2.0.zip

2、部署应用

3、修改
weibo4j.Weibo这个类
public static String CONSUMER_KEY = "140650463";
	public static String CONSUMER_SECRET = "XXXXXXXXXXXXXXX";
你自己的应用appkey


4、修改
weibo4j\WebRoot\call.jsp

RequestToken resToken=weboauth.request("http://localhost:8080/weibo/callback.jsp");


5、修改
weibo4j\WebRoot\WEB-INF\web.xml
 <welcome-file>call.jsp</welcome-file>


6、访问这个应用

7、获取
weibo4j\WebRoot\callback.jsp

out.println(accessToken.getToken());
out.println(accessToken.getTokenSecret());

打印出来的值

并保持session值:session.setAttribute("accessToken", accessToken);


8、新建页面
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page
	language="java"
	import="weibo4j.*"
%>
<%@ page
	language="java"
	import="weibo4j.http.*"
%>
<%
	AccessToken accessToken = (AccessToken) session
			.getAttribute("accessToken");
	try {
		String screen_name = "快乐每一天Joe";
		Weibo weibo = new Weibo();
		weibo.setToken(accessToken.getToken(), accessToken
				.getTokenSecret());
		User user = weibo.showUser(screen_name);
		String userInfo = user.toString();
		userInfo = userInfo.substring(userInfo.indexOf("{") + 1,
				userInfo.lastIndexOf("}"));
		String[] arrStrs = userInfo.split(",");
		for (int i = 0; i < arrStrs.length; i++) {
			out.println(arrStrs[i] + "<br/>");
		}
	} catch (WeiboException e) {
		e.printStackTrace();
	}
%>

你可能感兴趣的:(java)