Android腾讯微博开放平台入门(四)发表一条文字微博

21 篇文章 0 订阅
4 篇文章 0 订阅

 

项目源代码:http://download.csdn.net/detail/mrlixirong/3804385

 

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.content.Context;

/**
 * 
 * @author mrlixirong
 * 
 *  2011-11-16
 *
 */

public class WeiboQQAPI {
	
	/**
	 * 
	 * @param oauth 成功授权的oauth
	 * @param content 发布内容
	 * @param clientip ip,可选
	 * @param jing 经度,可选
	 * @param wei 纬度,可选
	 * @param context 用于获取联网管理设备
	 * @return 此处直接返回相应代码
	 */
	public static String sendMessage(Oauth oauth,String content,String clientip,String jing,String wei,Context context){
		
		String url="http://open.t.qq.com/api/t/add";
		oauth.oauth_nonce=Utils.getRandomString(32);
		oauth.oauth_timestamp=String.valueOf(System.currentTimeMillis()/1000);
		//
		BaseString bs=new BaseString();
		bs.setHttpMethod("POST");
		bs.setURL(url);
		bs.addParams("oauth_consumer_key", oauth.oauth_consumer_key);
		bs.addParams("oauth_token", oauth.oauth_token);
		bs.addParams("oauth_nonce", oauth.oauth_nonce);
		bs.addParams("oauth_timestamp", oauth.oauth_timestamp);
		bs.addParams("oauth_signature_method","HMAC-SHA1");
		bs.addParams("oauth_version","1.0");
		bs.addParams("format", "json");
		bs.addParams("content", URLEncoder.encode(content));
		bs.addParams("clientip", clientip);
		bs.addParams("jing", jing);
		bs.addParams("wei", wei);
		oauth.oauth_signature=Utils.getSignature(bs.getBaseString(),oauth.oauth_consumer_secret+"&"+oauth.oauth_token_secret);
		//
		try {
			DefaultHttpClient httpClient=new DefaultHttpClient();
			/*
			 * 一下屏蔽内容为代理设置相关
			ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(cm!=null){
				NetworkInfo  activeNetInfo = cm.getActiveNetworkInfo();
				if(!activeNetInfo.getTypeName().equals("WIFI")){
					String host=Proxy.getDefaultHost();
					int port = Proxy.getPort(context);  
					HttpHost httpHost = new HttpHost(host, port); 
					httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,httpHost);
				}
			}
			*/

			HttpPost post=new HttpPost(url);
			post.getParams().setParameter("http.socket.timeout",new Integer(20000));
			List<NameValuePair> params = new ArrayList<NameValuePair>();  
			params.add(new BasicNameValuePair("oauth_consumer_key",oauth.oauth_consumer_key)); 
			params.add(new BasicNameValuePair("oauth_token",oauth.oauth_token)); 
			params.add(new BasicNameValuePair("oauth_version","1.0")); 
			params.add(new BasicNameValuePair("oauth_signature_method","HMAC-SHA1")); 
			params.add(new BasicNameValuePair("oauth_signature",oauth.oauth_signature));
			params.add(new BasicNameValuePair("oauth_nonce",oauth.oauth_nonce)); 
			params.add(new BasicNameValuePair("oauth_timestamp",oauth.oauth_timestamp)); 
			params.add(new BasicNameValuePair("format","json")); 
			params.add(new BasicNameValuePair("content",content)); 
			params.add(new BasicNameValuePair("clientip",clientip)); 
			params.add(new BasicNameValuePair("jing",jing)); 
			params.add(new BasicNameValuePair("wei",wei));
			
			HttpEntity httpEntity=new UrlEncodedFormEntity(params,HTTP.UTF_8);
			post.setEntity(httpEntity);  	
			
			HttpResponse response=httpClient.execute(post);
			StatusLine sl=response.getStatusLine();
			int code = sl.getStatusCode();
			if(code==200){
				String result = EntityUtils.toString(response.getEntity());  
				return result;
			}else{
				return "false";
			}
		} catch (Exception e) {
			return "false";
		}  
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值