新浪微博(二十六)发布一条微博(不含图片)

来源:互联网 发布:期货软件如何看仓差 编辑:程序博客网 时间: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();}}

原创粉丝点击