<pre name="code" class="java">
public class SinaActivity extends Activity {
WebView webView;
String url2;
HttpPost httpPost;
HttpResponse response;
HttpEntity entity;
HttpClient httpClient;
String access_token;
Button button;
String App_Key = "输入你申请到的Key";
String Rediert_url = "输入你的回调网址";
String App_Secret = "请输入你的APP_secret";
String code;
public static final String authUrl = "https://open.weibo.cn/oauth2/authorize";// 新浪微博授权地址
public static final String authUrl2 = "https://open.weibo.cn/oauth2/access_token";// 新浪微博授权地址
private String url1 = authUrl
+ "?client_id="
+ App_Key
+ "&response_type=code&forcelogin=true&display=mobile&redirect_uri="
+ Rediert_url;
private static final String requesturl = "https://api.weibo.com/2/statuses/update.json";// 新浪发送消息接口
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sina);
button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
button.setText(share());
System.out.println(share());
}
});
webView = (WebView) findViewById(R.id.sina);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Map<String, String> data = pareseUrl(url);
if (data.containsKey("error")) {
finish();
} else {
code = data.get("code");
url2 = authUrl2 + "?client_id=" + App_Key
+ "&client_secret=" + App_Secret
+ "&grant_type=authorization_code&redirect_uri="
+ Rediert_url + "&code=" + code;
login();
}
return true;
}
});
webView.loadUrl(url1);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@SuppressWarnings("deprecation")
private Map<String, String> pareseUrl(String url) {
Map<String, String> dataMap = new HashMap<String, String>();
try {
URL mURL = new URL(url);
String s = mURL.getQuery();
if (s != null) {
String array[] = s.split("&");
for (String parameter : array) {
String v[] = parameter.split("=");
if (v.length >= 2)
dataMap.put(URLDecoder.decode(v[0]),
URLDecoder.decode(v[1]));
else
dataMap.put(URLDecoder.decode(v[0]), "");
}
}
s = mURL.getRef();
if (s != null) {
String array[] = s.split("&");
for (String parameter : array) {
String v[] = parameter.split("=");
if (v.length >= 2)
dataMap.put(URLDecoder.decode(v[0]),
URLDecoder.decode(v[1]));
else
dataMap.put(URLDecoder.decode(v[0]), "");
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return dataMap;
}
public void login() {
httpClient = new DefaultHttpClient();
String result = null;
try {
httpPost = new HttpPost(url2);
List<NameValuePair> data = new ArrayList<NameValuePair>();
httpPost.setEntity(new UrlEncodedFormEntity(data, "UTF-8"));
response = httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity(), "UTF-8");
JSONObject datas;
try {
datas = new JSONObject(result.toString());
access_token = datas.getString("access_token");// 兑换码
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}//该函数为了从URL中获取数据
public String share() {
httpClient = new DefaultHttpClient();
String result = null;
try {
httpPost = new HttpPost(requesturl);
List<NameValuePair> data = new ArrayList<NameValuePair>();
try {
data.add(new BasicNameValuePair("status", "输入你想上传的内容"));//上传你需要分享的内容
data.add(new BasicNameValuePair("access_token", access_token));
} catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}
httpPost.setEntity(new UrlEncodedFormEntity(data, "UTF-8"));
response = httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(result);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return result;
}
}
activity_sina.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/Button"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="share"/>
<WebView
android:id="@+id/sina"
android:layout_width="450dp"
android:layout_height="fill_parent" />
</LinearLayout>
<span style="color: rgb(51, 51, 51); font-family: 'Microsoft YaHei', 宋体, Arial, Helvetica, san-serif; font-size: 14px; line-height: 21px;">http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6 详细的内容可以去新浪微博的官方文档进行查看</span>
<pre name="code" class="java">