新浪微博(十八)获取、转发、收藏、发布微博的工具类(WeiBo.java类)

来源:互联网 发布:微分销系统开发源码 编辑:程序博客网 时间:2024/05/17 07:16

这个类里面封装了获取当前登录用户及其所关注用户的最新微博各种操作,是前面的叫做微博大厅(WeiBoContentActivity类)的助手类。

package tianyi.sina.weibo;import java.util.ArrayList;import java.util.List;import tianyi.helper.Parameter;import tianyi.helper.SyncHttp;import tianyi.helper.TextUtil;import tianyi.oauth.OauthHelper;public class Weibo {private String response = null;private String param;private List<Parameter> params = new ArrayList<Parameter>();/** * 获取微博信息列表 "statuses/friends_timeline.json" format 返回数据格式参数 url httpMethod * access_token_key access_secret format: 返回数据的格式 是(json或xml) pageflag * 分页标识(0:第一页,1:向下翻页,2向上翻页) reqnum: 每次请求记录的条数(1-70条) pagetime: 本页起始时间(第一页 * 时填0,继续翻页:填上一次请求返回的最后一条记录时间) count */public String getWeiBoList(String url, String httpMethod,String access_token_key, String access_secret, String format,String pageflag, String reqnum, String pagetime, String count) {try {// 获取请求参数if (!TextUtil.isEmpty(access_token_key)) {params.add(new Parameter("oauth_token",encode(access_token_key)));}if (!TextUtil.isEmpty(format)) {params.add(new Parameter("format", encode(format)));}if (!TextUtil.isEmpty(pageflag)) {params.add(new Parameter("pageflag", encode(pageflag)));}if (!TextUtil.isEmpty(reqnum)) {params.add(new Parameter("reqnum", encode(reqnum)));}if (!TextUtil.isEmpty(pagetime)) {params.add(new Parameter("pagetime", encode(pagetime)));}if (!TextUtil.isEmpty(count)) {params.add(new Parameter("count", encode(count)));}String param = new OauthHelper().getPostParams(url, httpMethod,access_secret, params);response = request(url, httpMethod, param);} catch (Exception e) {e.printStackTrace();}return response;}private String encode(String data) {return new OauthHelper().encode(data);}/** * 请求方式的选择 *  * @param url * @param httpMethod * @param params * @return * @throws Exception */private String request(String url, String httpMethod, String param)throws Exception {SyncHttp http = new SyncHttp();if (httpMethod == "POST")response = http.httpPost(url, param);else if (httpMethod == "GET")response = http.httpGet(url, param);return response;}/** * 收藏一条微博 *  */public String addfavorite(String url, String httpMethod,String access_token_key, String access_secret, String id,String format) {try {// 获取请求参数if (!TextUtil.isEmpty(access_token_key)) {params.add(new Parameter("oauth_token",encode(access_token_key)));}if (!TextUtil.isEmpty(id)) {params.add(new Parameter("id", encode(id)));}if (!TextUtil.isEmpty(format)) {params.add(new Parameter("format", encode(format)));}param = new OauthHelper().getPostParams(url, httpMethod,access_secret, params);response = request(url, httpMethod, param);} catch (Exception e) {e.printStackTrace();}return response;}/** * 根据Id获取一条微博内容 */public String getOneWeiBo(String url, String httpMethod,String access_token_key, String access_secret, String id,String format) {try {// 获取请求参数if (!TextUtil.isEmpty(access_token_key)) {params.add(new Parameter("oauth_token",encode(access_token_key)));}if (!TextUtil.isEmpty(id)) {params.add(new Parameter("id", encode(id)));}if (!TextUtil.isEmpty(format)) {params.add(new Parameter("format", encode(format)));}param = new OauthHelper().getPostParams(url, httpMethod,access_secret, params);System.out.println("参数===" + param);response = request(url, httpMethod, param);} catch (Exception e) {e.printStackTrace();}return response;}/** * 发表一条普通微博 *  * @param url * @param httpMethod * @param access_token_key * @param access_secret * @param status * @param format * @param content * @param clientip * @return String response */public String update(String url, String httpMethod,String access_token_key, String access_secret, String status,String format, String content, String clientip) {try {// 获取请求参数if (!TextUtil.isEmpty(access_token_key)) {params.add(new Parameter("oauth_token",encode(access_token_key)));}if (!TextUtil.isEmpty(status)) {params.add(new Parameter("status", encode(status)));}if (!TextUtil.isEmpty(format)) {params.add(new Parameter("format", encode(format)));}if (!TextUtil.isEmpty(content)) {params.add(new Parameter("content", encode(content)));}if (!TextUtil.isEmpty(clientip)) {params.add(new Parameter("clientip", encode(clientip)));}param = new OauthHelper().getPostParams(url, httpMethod,access_secret, params);response = request(url, httpMethod, param);} catch (Exception e) {e.printStackTrace();}return response;}/** * 评论一条微博 *  */public String comment(String url, String httpMethod,String access_token_key, String access_secret, String format,String content, String comment, String clientip, String id,String reid) {try {// 获取请求参数if (!TextUtil.isEmpty(access_token_key)) {params.add(new Parameter("oauth_token",encode(access_token_key)));}if (!TextUtil.isEmpty(format)) {params.add(new Parameter("format", encode(format)));}if (!TextUtil.isEmpty(content)) {params.add(new Parameter("content", encode(content)));}if (!TextUtil.isEmpty(clientip)) {params.add(new Parameter("clientip", encode(clientip)));}if (!TextUtil.isEmpty(id)) {params.add(new Parameter("id", encode(id)));}if (!TextUtil.isEmpty(reid)) {params.add(new Parameter("reid", encode(reid)));}if (!TextUtil.isEmpty(comment)) {params.add(new Parameter("comment", encode(comment)));}param = new OauthHelper().getPostParams(url, httpMethod,access_secret, params);response = request(url, httpMethod, param);} catch (Exception e) {e.printStackTrace();}return response;}}


原创粉丝点击