新浪微博(二十六)发布一条微博(不含图片)
来源:互联网 发布:期货软件如何看仓差 编辑:程序博客网 时间:2024/04/29 23:16
由于我使用的是Oauth1.0进行开发的,因此,发布一条微博时,所使用到的URL地址是新浪API1.0版本的URL。这一点一点要注意。另外,我对发布一条微博和转发一条微博的处理是一样的,即先获取包含微博的JSONObject,然后获得text,最后发送给服务器,显然发送的信息只是text。你要是感兴趣可以在此基础上添加更多的可能性
发布微博的功能的实现相对比较简单,需要说明的有两点:1.获取用户输入的微博字符串的方法:editText.getText().toString()。2.封装一个发布微博的函数。
代码:
/* * 很明显你要参照的是1.0版本的官方API才能看明白本代码 * 转发一条微博消息。请求必须用POST方式提交。 * URL:http://api.t.sina.com.cn/statuses/repost.(json%7cxml) * 支持格式:XML/JSON * HTTP请求方式:POST * 是否需要登录:true * 为防止重复提交,当用户发布的微博消息与上次成功发布的微博消息内容一样时,将返回400错误 * 为了与部分老版本的SDK兼容,该接口还有另外一种调用方式: * http://api.t.sina.com.cn/statuses/retweet/:id.format * 其中,:id参数以REST风格的形式出现,表示要转发的微博消息ID */package tianyi.sina.weibo.operator;import org.json.JSONException;import org.json.JSONObject;import tianyi.oauth.OauthUtils;import tianyi.sina.R;import tianyi.sina.weibo.Weibo;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.Toast;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;public class ZhuanFaActivity extends Activity {private String httpMethod = "GET";private String url;private String jsonData;private JSONObject jsonObj = null;private SharedPreferences pres = null;private Button zhuanfa;// 要转发的微博的内容private String text = null;private EditText editText = null;private CheckBox checkBox = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(tianyi.sina.R.layout.operator_zhuanfa);zhuanfa = (Button) findViewById(R.id.operator_zhuanfa_button1);zhuanfa.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {// 为发送微博准备必要的数据text = getData();// 发送微博zhuanFaWeiBo(text);finish();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});editText = (EditText) findViewById(R.id.operator_zhuanfa_editText1);checkBox = (CheckBox) findViewById(R.id.operator_zhuanfa_checkBox1);checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stub}});}private String getData() throws JSONException {Intent intent = getIntent();String weiBoID = intent.getStringExtra("weiBoID");pres = getSharedPreferences("parameters", Context.MODE_PRIVATE);String sina_access_token = pres.getString("sina_access_token", "");String sina_access_secret = pres.getString("sina_access_secret", "");url = "http://api.t.sina.com.cn/statuses/show/:" + weiBoID + ".json";// url = "http://api.t.sina.com.cn/statuses/retweet/:" + weiBoID +// ".json";/* * 作用:根据Id获取一条微博内容 URL:https://api.weibo.com/2/statuses/show.json * 支持格式:JSON HTTP请求方式:GET 是否需要登录:是 访问级别:普通接口 频次限制:是 参数: access_token * 需要获取的微博id * 旧版本的URL:http://api.t.sina.com.cn/statuses/retweet/:id.format */jsonData = new Weibo().getOneWeiBo(url, httpMethod, sina_access_token,sina_access_secret, null, null);try {jsonObj = new JSONObject(jsonData);} catch (JSONException e) {e.printStackTrace();}text = jsonObj.getString("text");return text;}/** * 转发微博 * * @param status */private void zhuanFaWeiBo(String text) {httpMethod = "POST";String sign = null;SharedPreferences pres = this.getSharedPreferences("parameters",Context.MODE_PRIVATE);String sina_access_token = pres.getString("sina_access_token", "");String sina_access_secret = pres.getString("sina_access_secret", "");url = "http://api.t.sina.com.cn/statuses/update.json";OauthUtils.getInstance().initSinaData();jsonData = new Weibo().submitOneWeibo(url, httpMethod,sina_access_token, sina_access_secret, text, null, null, null);if ("-1".equals(jsonData)) {sign = "发送失败!";} else {try {jsonObj = new JSONObject(jsonData);} catch (JSONException e) {e.printStackTrace();}if ("400".equals(jsonData)) {sign = "重复发送!";} elsetry {if (jsonObj.getString("text").equals(text)) {sign = "sina发送成功!";} else {sign = "发送失败!";}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Toast.makeText(ZhuanFaActivity.this, sign, Toast.LENGTH_SHORT).show();}}
- 新浪微博(二十六)发布一条微博(不含图片)
- 新浪微博(二十七)发布微博(含图片)
- 新浪微博发布一条微博和图片的接口调用实例
- 新浪微博开发之二十六(上拉加载)
- 新浪微博(二十三)操作一条微博(SingleWeiBoActivity 类)
- 新浪微博(二十四)评论、转发、收藏一条微博
- 新浪微博开发之十六(封装网络工具)
- 【分享】一万条新浪微博原文(含用户UID)
- 新浪微博图片
- 新浪微博发布效果
- 新浪微博分享图片不改变问题
- 新浪微博(十六)微博界面(WeiBoFrameActivity类)
- 分享到新浪微博(二)
- 新浪微博(二十)使用ListView显示网络图片(SearchAdapter类)
- Android ProgressBar 自定义样式(二),仿原来新浪微博图片加载样式
- 仿新浪微博2014之登陆界面二(异步加载图片和缓存)
- 仿新浪微博2014之登陆界面二(异步加载图片和缓存)
- 新浪微博登录界面上下拉伸图片--第三方开源--PullToZoomListViewEx(二)
- Nginx + https + 免费SSL证书配置指南
- SQL中char、varchar、text和nchar、nvarchar、ntext的区别
- CMOS与TTL电路的区别
- OnInitUpdate、OnUpdate、OnDraw与OnPaint!
- Spring MVC 入门
- 新浪微博(二十六)发布一条微博(不含图片)
- 利用C# 5 新特性 异步操作UI
- 滤波电容,去耦电容,旁路电容的区别
- AX控件IE相关注册表修改封装
- S3C2440 LCD字符显示
- 反射相关
- VS2010配色方案
- 磁珠的作用以及和电感的区别
- 一个游戏程序员的学习资料