Android新浪微博获取评论信息、发表评论、转发微博等.

本文介绍了如何使用OAuth认证获取微博评论信息、对微博进行评论及转发微博的方法,并提供了实现代码及应用展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先前面一节中说过,获取用户的微博信息,这里简单介绍下获取微博的评论信息,以及对微博进行评论,转发微博等.

OAuth认证,这里就不多说了,

我说名一下接口:

获取微博的评论列表接口: http://api.t.sina.com.cn/statuses/comments.json

我们这里需要把微博ID做为参数请求,这个ID我们可以根据前面一节解析用户的微博信息得到.

对微博进行评论接口:http://api.t.sina.com.cn/statuses/comment.json

我们需要把微博的id,与我们的评论comment做为参数进行请求.

微博转发接口:http://api.t.sina.com.cn/statuses/repost.json

这里我们只需要微博的id.

下面是实现部分代码:

/***
	 * 获取用户的评论
	 * 
	 * @param requstURL
	 *            请求url
	 * @param blog_id
	 *            微博id
	 * @return JSON(String)
	 * @throws OAuthMessageSignerException
	 * @throws OAuthExpectationFailedException
	 * @throws OAuthCommunicationException
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	public static String getJSONBlogsComments(String requstURL, String blog_id)
			throws OAuthMessageSignerException,
			OAuthExpectationFailedException, OAuthCommunicationException,
			ClientProtocolException, IOException, JSONException {
		String jsonArray = null;
		HttpResponse httpResponse;
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(requstURL);
		getTokenAndTokenSecret();
		CommonsHttpOAuthConsumer authConsumer = new CommonsHttpOAuthConsumer(
				Constant.weiBo.consumerKey, Constant.weiBo.consumerSecret);
		authConsumer.setTokenWithSecret(token, tokenSecret);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs
				.add(new BasicNameValuePair(Constant.weiBoTag.id, blog_id));
		httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
		httpPost.getParams().setBooleanParameter(
				CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

		authConsumer.sign(httpPost);
		httpResponse = client.execute(httpPost);

		if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {
			jsonArray = EntityUtils.toString(httpResponse.getEntity());
		}
		Log.i(Constant.TAG, jsonArray);
		return jsonArray;
	}

		/***
	 * 评论一条微博
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @throws OAuthCommunicationException
	 * @throws OAuthExpectationFailedException
	 * @throws OAuthMessageSignerException
	 */
	public static boolean CommentBlogByID(String blog_ID, String comment,
			String requstURL) throws ClientProtocolException, IOException,
			OAuthMessageSignerException, OAuthExpectationFailedException,
			OAuthCommunicationException {
		HttpResponse httpResponse;
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(requstURL);
		getTokenAndTokenSecret();
		CommonsHttpOAuthConsumer authConsumer = new CommonsHttpOAuthConsumer(
				Constant.weiBo.consumerKey, Constant.weiBo.consumerSecret);
		authConsumer.setTokenWithSecret(token, tokenSecret);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs
				.add(new BasicNameValuePair(Constant.weiBoTag.id, blog_ID));
		nameValuePairs.add(new BasicNameValuePair(Constant.weiBoTag.comment,
				comment));

		httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
		httpPost.getParams().setBooleanParameter(
				CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
		authConsumer.sign(httpPost);

		httpResponse = client.execute(httpPost);

		if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {
			return true;
		}
		return false;
	}

	/****
	 * 微博转发
	 * 
	 * @param blog_ID
	 *            微博id
	 * @param requstURL
	 *            请求URL
	 * @return
	 * @throws OAuthMessageSignerException
	 * @throws OAuthExpectationFailedException
	 * @throws OAuthCommunicationException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static boolean TranspondBlog(String blog_ID, String requstURL)
			throws OAuthMessageSignerException,
			OAuthExpectationFailedException, OAuthCommunicationException,
			ClientProtocolException, IOException {
		HttpResponse httpResponse;
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(requstURL);
		getTokenAndTokenSecret();
		CommonsHttpOAuthConsumer authConsumer = new CommonsHttpOAuthConsumer(
				Constant.weiBo.consumerKey, Constant.weiBo.consumerSecret);
		authConsumer.setTokenWithSecret(token, tokenSecret);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs
				.add(new BasicNameValuePair(Constant.weiBoTag.id, blog_ID));

		httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
		httpPost.getParams().setBooleanParameter(
				CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
		authConsumer.sign(httpPost);

		httpResponse = client.execute(httpPost);

		if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {
			return true;
		}
		return false;
	}


其实主要是Oauth认证,我们只需要相应的Token 和Tokensecret,这些接口没有什么,相信大家看过新浪API都会的.

下面是实现后的效果:

获取用微博的评论列表 获取用户的评论列表

微博评论界面 刚评论的信息

转发的微博.

应用正在开发中,界面很丑陋,所以请见谅.

好了对于微博的一些应用就介绍这么多了,如果有哪里写的不足,您不必高抬贵手,请留言指出.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值