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

     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.

   

    下面是实现部分代码:

   

  1. /*** 
  2.      * 获取用户的评论 
  3.      *  
  4.      * @param requstURL 
  5.      *            请求url 
  6.      * @param blog_id 
  7.      *            微博id 
  8.      * @return JSON(String) 
  9.      * @throws OAuthMessageSignerException 
  10.      * @throws OAuthExpectationFailedException 
  11.      * @throws OAuthCommunicationException 
  12.      * @throws ClientProtocolException 
  13.      * @throws IOException 
  14.      * @throws JSONException 
  15.      */  
  16.     public static String getJSONBlogsComments(String requstURL, String blog_id)  
  17.             throws OAuthMessageSignerException,  
  18.             OAuthExpectationFailedException, OAuthCommunicationException,  
  19.             ClientProtocolException, IOException, JSONException {  
  20.         String jsonArray = null;  
  21.         HttpResponse httpResponse;  
  22.         HttpClient client = new DefaultHttpClient();  
  23.         HttpPost httpPost = new HttpPost(requstURL);  
  24.         getTokenAndTokenSecret();  
  25.         CommonsHttpOAuthConsumer authConsumer = new CommonsHttpOAuthConsumer(  
  26.                 Constant.weiBo.consumerKey, Constant.weiBo.consumerSecret);  
  27.         authConsumer.setTokenWithSecret(token, tokenSecret);  
  28.   
  29.         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  30.         nameValuePairs  
  31.                 .add(new BasicNameValuePair(Constant.weiBoTag.id, blog_id));  
  32.         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));  
  33.         httpPost.getParams().setBooleanParameter(  
  34.                 CoreProtocolPNames.USE_EXPECT_CONTINUE, false);  
  35.   
  36.         authConsumer.sign(httpPost);  
  37.         httpResponse = client.execute(httpPost);  
  38.   
  39.         if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {  
  40.             jsonArray = EntityUtils.toString(httpResponse.getEntity());  
  41.         }  
  42.         Log.i(Constant.TAG, jsonArray);  
  43.         return jsonArray;  
  44.     }  
  45.   
  46.         /*** 
  47.      * 评论一条微博 
  48.      *  
  49.      * @throws IOException 
  50.      * @throws ClientProtocolException 
  51.      * @throws OAuthCommunicationException 
  52.      * @throws OAuthExpectationFailedException 
  53.      * @throws OAuthMessageSignerException 
  54.      */  
  55.     public static boolean CommentBlogByID(String blog_ID, String comment,  
  56.             String requstURL) throws ClientProtocolException, IOException,  
  57.             OAuthMessageSignerException, OAuthExpectationFailedException,  
  58.             OAuthCommunicationException {  
  59.         HttpResponse httpResponse;  
  60.         HttpClient client = new DefaultHttpClient();  
  61.         HttpPost httpPost = new HttpPost(requstURL);  
  62.         getTokenAndTokenSecret();  
  63.         CommonsHttpOAuthConsumer authConsumer = new CommonsHttpOAuthConsumer(  
  64.                 Constant.weiBo.consumerKey, Constant.weiBo.consumerSecret);  
  65.         authConsumer.setTokenWithSecret(token, tokenSecret);  
  66.   
  67.         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  68.         nameValuePairs  
  69.                 .add(new BasicNameValuePair(Constant.weiBoTag.id, blog_ID));  
  70.         nameValuePairs.add(new BasicNameValuePair(Constant.weiBoTag.comment,  
  71.                 comment));  
  72.   
  73.         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));  
  74.         httpPost.getParams().setBooleanParameter(  
  75.                 CoreProtocolPNames.USE_EXPECT_CONTINUE, false);  
  76.         authConsumer.sign(httpPost);  
  77.   
  78.         httpResponse = client.execute(httpPost);  
  79.   
  80.         if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {  
  81.             return true;  
  82.         }  
  83.         return false;  
  84.     }  
  85.   
  86.     /**** 
  87.      * 微博转发 
  88.      *  
  89.      * @param blog_ID 
  90.      *            微博id 
  91.      * @param requstURL 
  92.      *            请求URL 
  93.      * @return 
  94.      * @throws OAuthMessageSignerException 
  95.      * @throws OAuthExpectationFailedException 
  96.      * @throws OAuthCommunicationException 
  97.      * @throws ClientProtocolException 
  98.      * @throws IOException 
  99.      */  
  100.     public static boolean TranspondBlog(String blog_ID, String requstURL)  
  101.             throws OAuthMessageSignerException,  
  102.             OAuthExpectationFailedException, OAuthCommunicationException,  
  103.             ClientProtocolException, IOException {  
  104.         HttpResponse httpResponse;  
  105.         HttpClient client = new DefaultHttpClient();  
  106.         HttpPost httpPost = new HttpPost(requstURL);  
  107.         getTokenAndTokenSecret();  
  108.         CommonsHttpOAuthConsumer authConsumer = new CommonsHttpOAuthConsumer(  
  109.                 Constant.weiBo.consumerKey, Constant.weiBo.consumerSecret);  
  110.         authConsumer.setTokenWithSecret(token, tokenSecret);  
  111.   
  112.         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  113.         nameValuePairs  
  114.                 .add(new BasicNameValuePair(Constant.weiBoTag.id, blog_ID));  
  115.   
  116.         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));  
  117.         httpPost.getParams().setBooleanParameter(  
  118.                 CoreProtocolPNames.USE_EXPECT_CONTINUE, false);  
  119.         authConsumer.sign(httpPost);  
  120.   
  121.         httpResponse = client.execute(httpPost);  
  122.   
  123.         if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {  
  124.             return true;  
  125.         }  
  126.         return false;  
  127.     }  
/***
	 * 获取用户的评论
	 * 
	 * @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都会的.

     

     下面是实现后的效果:

                        

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

                     

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

        

                                   转发的微博.

   

posted on 2015-01-21 08:28  车车12138  阅读(641)  评论(0编辑  收藏  举报