项目源代码: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";
}
}
}