首先前面一節中說過,擷取使用者的微博資訊,這裡簡單介紹下擷取微博的評論資訊,以及對微博進行評論,轉寄微博等.
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都會的.
下面是實現後的效果:
擷取用微博的評論列表 擷取使用者的評論列表
微博評論介面 剛評論的資訊
轉寄的微博.
應用正在開發中,介面很醜陋,所以請見諒.
好了對於微博的一些應用就介紹這麼多了,如果有哪裡寫的不足,您不必高抬貴手,請留言指出.