试试看
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case HIDE_PROGRESS_DIALOG:{
hideProgressDialog();
}break;
case PROCESS_TOKEN:{
processToken(url);
}break;
}
}
}
@Override
public void create() {
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.setBackgroundColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
mainLayout.setLayoutParams(lp);
webView = new WebView(this);
webView.setLayoutParams(lp);
mainLayout.addView(webView);
setContentView(mainLayout);
webView.getSettings().setJavaScriptEnabled(true);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:{
}break;
case MotionEvent.ACTION_UP:{
if (!v.hasFocus()) {
v.requestFocus();
}
}break;
}
return false;
}
});
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(CALLBACK_URL)) {
TwitterAuthorizationActivity.this.url = url;
mHandler.sendEmptyMessage(PROCESS_TOKEN);
return true;
}else if (url.startsWith("authorize")) {
return false;
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
}
});
mTwitter = new TwitterFactory().getInstance();
mHttpOauthConsumer = new CommonsHttpOAuthConsumer(twitterConsumerKey, twitterSecretKey);
mHttpOauthprovider = new CommonsHttpOAuthProvider("https://twitter.com/oauth/request_token",
"https://twitter.com/oauth/access_token",
"https://twitter.com/oauth/authorize");
try{
authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);
}catch(OAuthCommunicationException oACEx){
Log.d("", "");
}catch(OAuthMessageSignerException oAMSEx){
Log.d("", "");
}catch(OAuthNotAuthorizedException oANAEx){
Log.d("", "");
}catch(OAuthExpectationFailedException oAEFEx){
Log.d("", "");
}
webView.loadUrl(authUrl);
}
public void processToken(String callbackUrl) {
final String verifier = getVerifier(callbackUrl);
int what = 1;
try {
mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier);
mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret());
mTwitter.setOAuthConsumer(twitterConsumerKey, twitterSecretKey);
mTwitter.setOAuthAccessToken(mAccessToken);
what = 0;
} catch (Exception e){
}
}
private String getVerifier(String callbackUrl) {
String verifier = "";
try {
callbackUrl = callbackUrl.replace("twitterapp", "http");
URL url = new URL(callbackUrl);
String query = url.getQuery();
String array[] = query.split("&");
for (String parameter : array) {
String v[] = parameter.split("=");
if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) {
verifier = URLDecoder.decode(v[1]);
break;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return verifier;
}
此解决方案使用Twitter4J库。