1. 从http://code.google.com/p/sina-weibo4android/downloads/list 下载 webo4android。
2.修改/src/weibo4android/Weibo.java CONSUMER_KEY参数和 CONSUMER_SECRET(填写在sina申请的 key和secret)。
基本配置就是这些。但是为了应对第三方浏览器授权后不跳转的问题,我用了网上 webview的解决方案。
源码如下
public class AndroidExample extends Activity {
/** Called when the activity is first created. */
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
System.setProperty("weibo4j.oauth.consumerSecret",
Weibo.CONSUMER_SECRET);
Button beginOuathBtn = (Button) findViewById(R.id.Button01);
/**这段代码用来 set webview的各种参数**/
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setSavePassword(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
/**用来 获取 webview的焦点否则点击后可能打开浏览器**/
webView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
webView.requestFocus();
return false;
}
});
/**重写 webview 读取网页时的进度行为**/
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
setTitle("稍等片刻,页面加载中..." + newProgress + "%");
setProgress(newProgress * 100);
if (newProgress == 100) {
setTitle(R.string.app_name);
}
}
});
beginOuathBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Weibo weibo = new Weibo();
RequestToken requestToken;
try {
requestToken = weibo
.getOAuthRequestToken("weibo4android://TestActivity");
/*
* weibo4android://TestActivity
*
* scheme = weibo4android
*
* TestActivity 是 点击授权后 回call的activity
*/
OAuthConstant.getInstance().setRequestToken(requestToken);
Uri uri = Uri.parse(requestToken.getAuthenticationURL()
+ "&display=mobile");
/**用webview 打开授权网页 取代源码中原先 **/
webView.loadUrl(uri.toString());
//用浏览器打开授权页面的方法。
// startActivity(new Intent(Intent.ACTION_VIEW, uri));
} catch (WeiboException e) {
e.printStackTrace();
}
}
});
}
}
接下来是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="进行登录授权" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></TextView>
<Button android:text="submit" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"></Button>
<WebView android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1" />
</LinearLayout>
/*
<WebView android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1" />
这段就是在layout xml 里添加添加web view 控件
*/
AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="weibo4android.androidexamples" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".AndroidExample">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OAuthActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="OAuthActivity" android:scheme="weibo4android" />
</intent-filter>
</activity>
<activity android:name=".TestActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="TestActivity" android:scheme="weibo4android" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
/*
<activity android:name=".TestActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="TestActivity" android:scheme="weibo4android" />
</intent-filter>
</activity>
这里是需要配置的回调activity
*/
代码在android 2.1下测试 没问题。