首先前面一节中说过,获取用户的微博信息,这里简单介绍下获取微博的评论信息,以及对微博进行评论,转发微博等.
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都会的.
下面是实现后的效果:
获取用微博的评论列表 获取用户的评论列表
微博评论界面 刚评论的信息
转发的微博.
应用正在开发中,界面很丑陋,所以请见谅.
好了对于微博的一些应用就介绍这么多了,如果有哪里写的不足,您不必高抬贵手,请留言指出.