AndroidOAuth认证例子




package com.example.xiangmu_sinaweibob;


import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.example.HTTP.DjHttpManager;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class DjOauth extends Activity
{
	private WebView webView;
	private 	String url1 = "https://api.weibo.com/oauth2/authorize?client_id=3171700220&redirect_uri=http://www.eastmoney.com&display=mobile";
	private   String accessToken ="https://api.weibo.com/oauth2/access_token";
	public String accessTokenString;
	public Runnable downRunnable;
	//private String accessToken = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionDataset";
	@SuppressLint("NewApi")
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads()
        .detectDiskWrites()
        .detectNetwork()   // or .detectAll() for all detectable problems
        .penaltyLog()
        .build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectLeakedSqlLiteObjects()
        .detectLeakedClosableObjects()
        .penaltyLog()
        .penaltyDeath()
        .build());
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.djoauth);

		
		
		
		webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				if (url.contains("code=")) {
					String codeStrA[] = url.split("=");
					System.out.println(codeStrA[1]);
					accessTokenString = codeStrA[1];

							System.out.println("accessTokenString");
							getAccessToken(accessTokenString);
									
					
				}
			}
		});
		webView.loadUrl(url1);
	}
	
	public void getAccessToken(String codeStrA) 
	{
		

		HttpPost httpPost = new HttpPost(accessToken);
		System.out.println("ffffffa");
		List paramsList = new ArrayList();
		paramsList.add(new BasicNameValuePair("client_id", "3171700220"));
		paramsList.add(new BasicNameValuePair("client_secret", "a49a7e4ea295e1d4d93eee7cc517c1dc"));
		paramsList.add(new BasicNameValuePair("grant_type", "authorization_code"));
		paramsList.add(new BasicNameValuePair("code", codeStrA));
		System.out.println(codeStrA);
		paramsList.add(new BasicNameValuePair("redirect_uri", "http://www.eastmoney.com"));
		
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(paramsList,HTTP.UTF_8));

		
			Thread.sleep(1000);
			HttpResponse httpResponse  = new DefaultHttpClient().execute(httpPost);
			
		
			if (httpResponse.getStatusLine().getStatusCode() == 200) 
			{
			String resulString = EntityUtils.toString(httpResponse.getEntity());
			System.out.println("resulString="+resulString);
			//将access_token的值从返回的jSOn数据中取出
			JSONObject jsonObject = new JSONObject(resulString);
			String access_tokenStr = jsonObject.getString("access_token");
			SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MainActivity.MODE_PRIVATE);
			SharedPreferences.Editor editor = sharedPreferences.edit();
			editor.putString("access_token", access_tokenStr);
			editor.commit();
			
			System.out.println(access_tokenStr);
			DjHttpManager djHttpManager = new DjHttpManager();
			djHttpManager.getUserAttentionWeibo(access_tokenStr);
			
			}	
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("e.toString() = "+e.toString());
			
		}
	}
	
	
}


你可能感兴趣的:(Android)