腾讯 iweibo Java API 1.2.1 补充头像和个人信息更新功能接口

腾讯 iweibo Java API 1.2.1 本身代码质量还算比较高的,虽然没有实现头像上载接口,但底层功能已经具备,只需少量修改即可。

 

负责用户信息更新和获取的API为 com.tencent.weibo.api.UserAPI,里面实现了info、otherInfo、infos等,却没有update_head接口,而既然可以发送图片微博,那参考图片微博的图片上载部分代码 com.tencent.weibo.api.TAPI 即可,以下是对UserAPI稍作添加和修改以实现头像更新。

 

以下红色部分为添加的内容

 

package com.tencent.weibo.api;

 

public class UserAPI extends BasicAPI
{

  private String userInfoUrl = apiBaseUrl + "/user/info";
  private String userOtherInfoUrl = apiBaseUrl + "/user/other_info";
  private String userInfosUrl = apiBaseUrl + "/user/infos";
  // 添加一个上载头像的接口路径,这个参考 http://dev.t.qq.com 接口介绍
  private String userUpdateHeadUrl = apiBaseUrl + "/user/update_head";
  private String userUpdateUrl = apiBaseUrl + "/user/update";
  ......

 

  // 函数内增加 userUpdateHeadUrl  设置

  public void setAPIBaseUrl(String apiBaseUrl)
  {
    this.apiBaseUrl = apiBaseUrl;
    userInfoUrl = apiBaseUrl + "/user/info";
    userOtherInfoUrl = apiBaseUrl + "/user/other_info";
    userInfosUrl = apiBaseUrl + "/user/infos";
    userUpdateHeadUrl = apiBaseUrl + "/user/update_head";

    userUpdateUrl = apiBaseUrl + "/user/update";
  }

 

  // 头像上载接口实现,3个参数,auth、format 和 picpatch

  public StringupdateHead(OAuth oAuth, String format, String picpath) throws Exception
  {
    QArrayList paramsList = new QArrayList();

    // 设置返回格式参数 "json" 或者 "xml"
    paramsList.add(new BasicNameValuePair("format", format));

    QArrayList pic = new QArrayList();

    // 传入 图片的绝对路径
    pic.add(new BasicNameValuePair("pic", picpath));
    return requestAPI.postFile(userUpdateHeadUrl, paramsList, pic, oAuth);
  }

 

// 个人信息更新接口

public String update(OAuth oAuth, String format, String nick, String sex, String year, String month, String day, String countrycode,
      String provincecode, String citycode, String introduction) throws Exception
{
  QArrayList paramsList = new QArrayList();

  paramsList.add(new BasicNameValuePair("format", format));
  paramsList.add(new BasicNameValuePair("nick", nick));
  paramsList.add(new BasicNameValuePair("sex", sex));
  paramsList.add(new BasicNameValuePair("year", year));
  paramsList.add(new BasicNameValuePair("month", month));
  paramsList.add(new BasicNameValuePair("day", day));
  paramsList.add(new BasicNameValuePair("countrycode", countrycode));
  paramsList.add(new BasicNameValuePair("provincecode", provincecode));
  paramsList.add(new BasicNameValuePair("citycode", citycode));
  paramsList.add(new BasicNameValuePair("introduction", introduction));

  return requestAPI.postContent(userUpdateUrl, paramsList, oAuth);
}

 

 

在接口处理前应该再加上文件检查代码,检查文件是否存在,再继续。

添加完毕后,重新生成 jar 文件即可在项目中使用了。

 

测试代码,授权部分略,经测试,成功实现头像更新。

 

/**
   * 更新自己的头像
   * @param oAuth
   */
  private static void testUserUpdateHead(OAuthV2 oAuth, String pic)
  {
    try
    {
      UserAPI user = new UserAPI(oAuth.getOauthVersion());
      log.info(user.updateHead(oAuth, "json",pic));
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

 

 本文链接

Q群讨论:236201801

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值