新浪微博分享实现(Web认证)

<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">

                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值