Package com.qq.open.weibo

Source Code of com.qq.open.weibo.WeiBoAddT

package com.qq.open.weibo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;

import com.qq.open.common.OpenQqConstants;
import com.qq.open.common.OpenQqUtils;
import com.qq.open.common.json.JSONException;
import com.qq.open.common.json.JSONObject;
import com.qq.open.weibo.bean.param.WeiBoAddParamBean;
import com.qq.open.weibo.bean.result.WeiBoAddResultBean;


/**
* 发布一条微博消息
*
* @author HaoLiang
* @version 0.1.1
*/
public class WeiBoAddT {

  /** QQ互联工具类 */
  private OpenQqUtils oqu = new OpenQqUtils();
 
  /**
   * 发布一条微博消息
   *
   * @param paramBean 参数Bean
   * @return 返回数据
   * @throws IOException
   * @throws JSONException
   */
  public WeiBoAddResultBean addT(WeiBoAddParamBean paramBean) throws IOException, JSONException {
   
    // 获取接口数据
    String jsonData = oqu.doPost(OpenQqConstants.WEIBO_ADD_T_URL, this.getAddWeiBoData(paramBean));
   
    return this.jsonToResultBean(jsonData);
  }
 
  /**
   * 获取发布微博的数据
   *
   * @param qqu
   * @param paramBean
   * @return 发布微博的数据
   * @throws UnsupportedEncodingException
   */
  private MultipartEntity getAddWeiBoData(WeiBoAddParamBean paramBean) throws UnsupportedEncodingException {
    MultipartEntity reqEntity = new MultipartEntity();
   
    // AccessToken
    reqEntity.addPart("access_token", new StringBody(paramBean
        .getAccessToken()));

    // AppId
    reqEntity.addPart("oauth_consumer_key", new StringBody(oqu.getConfigValue("qq.appid")));

    // Openid
    reqEntity.addPart("openid", new StringBody(paramBean.getOpenId()));

    // 定义API返回的数据格式
    reqEntity.addPart("format", new StringBody("json"));
   
    // 微博内容
    reqEntity.addPart("content", new StringBody(paramBean.getContent(), Charset.forName("UTF-8")));
   
    // 用户ip
    if (oqu.isNotNull(paramBean.getClientIp())) {
      reqEntity.addPart("clientip", new StringBody(paramBean.getClientIp()));
    }
   
    // 用户所在地理位置的经度
    if (oqu.isNotNull(paramBean.getLongitude())) {
      reqEntity.addPart("jing", new StringBody(paramBean.getLongitude()));
    }
   
    // 用户所在地理位置的纬度
    if (oqu.isNotNull(paramBean.getLatitude())) {
      reqEntity.addPart("wei", new StringBody(paramBean.getLatitude()));
    }
   
    // 同步到QQ空间标识   0:同步 1:不同步
    if (oqu.isNotNull(paramBean.getSyncFlag())) {
      reqEntity.addPart("syncflag", new StringBody(paramBean.getSyncFlag()));
    }
   
    return reqEntity;
  }
 
 
  /**
   * 把接口返回的json数据转换成Bean
   *
   * @param jsonData 接口返回的数据
   * @return 转换后的数据
   * @throws JSONException
   */
  private WeiBoAddResultBean jsonToResultBean(String jsonData) throws JSONException {
 
    WeiBoAddResultBean resultBean = new WeiBoAddResultBean();
   
    JSONObject jsonObj = new JSONObject(jsonData);
   
    // 添加微博失败的场合
    if (jsonObj.getInt("ret") != 0) {
      // 错误标识
      resultBean.setErrorFlg(true);
     
      // 错误编号
      resultBean.setErrorCode(jsonObj.get("errcode").toString());
     
      // 错误信息
      resultBean.setErrorMes(jsonObj.getString("msg"));
     
    } else {
      // 获取微博数据
      JSONObject jsonDataObj = new JSONObject(jsonObj.getJSONObject("data").toString());
     
      // 获取微博ID
      resultBean.setAddWeiBoId(jsonDataObj.get("id").toString());
     
      // 获取微博发布时间
      resultBean.setAddWeiBoDate(oqu.timeStampToDate(jsonDataObj.get("time").toString()));
    }
   
    return resultBean;
  }
 
}
TOP

Related Classes of com.qq.open.weibo.WeiBoAddT

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.