Android新浪微博擷取評論資訊、發表評論、轉寄微博等.

來源:互聯網
上載者:User

     首先前面一節中說過,擷取使用者的微博資訊,這裡簡單介紹下擷取微博的評論資訊,以及對微博進行評論,轉寄微博等.

     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都會的.

     

     下面是實現後的效果:

                        

              擷取用微博的評論列表                                            擷取使用者的評論列表

                     

              微博評論介面                                                   剛評論的資訊

        

                                   轉寄的微博.

         應用正在開發中,介面很醜陋,所以請見諒.

         好了對於微博的一些應用就介紹這麼多了,如果有哪裡寫的不足,您不必高抬貴手,請留言指出.

    

 

    

   

   

   

    

    

 

    

    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.