新浪微博发布一条微博和图片的接口调用实例

时间:2021-10-21 09:14:40

新浪微博接口文档:  http://open.weibo.com/wiki/2/statuses/upload  描述


调用示例:

/**
* 发布一条有图片的微博
*/
public void sendSinaWeiboWithPic(String content,String imgUrl,String accessToken){

HttpClient httpClient = new HttpClient();
httpClient.setToken(accessToken); //设置accessToken

byte[] imageBytes = ImgUtil.getImageBinary(imgUrl); //根据图片url得到图片的字节数组

PostParameter[] params = new PostParameter[2];
params[0] = new PostParameter("status", content);
params[1] = new PostParameter("pic", "");

try {

ImageItem item = new ImageItem(imageBytes);
Response rsp = httpClient.multPartURL(send_weibo_pic_url, params, item);
System.out.println(rsp);
} catch (WeiboException e) {
System.out.println(e.getMessage());
}

}

发布一条只带文本的微博:
 

 

/**
*发布一条微博信息
*/
public void sendSinaWeibo(String content,String accessToken){
HttpClient httpClient = new HttpClient();

httpClient.setToken(accessToken);

PostParameter param1 = new PostParameter("status", content);

PostParameter[] params = new PostParameter[2];
params[0] = param1;

try {

Response rsp = httpClient.post(send_weibo_url, params);
System.out.println(rsp);
} catch (WeiboException e) {
System.out.println(e.getMessage());
}
}