Android新浪微博获取评论信息、发表评论、转发微博等
首先前面一节中说过,获取用户的微博信息,这里简单介绍下获取微博的评论信息,以及对微博进行评论,转发微博等.
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;
- }
/*** * 获取用户的评论 * * @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都会的.
下面是实现后的效果:
获取用微博的评论列表 获取用户的评论列表
微博评论界面 刚评论的信息
转发的微博.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。