我尝试创建 Twitter 客户端,现在我通过 OAuth 协议处理授权。我已经创建了“登录”按钮来进入 WebView 并加载 twitter 授权 URL,这是工作。但是,当授权被成功接受并且 Twitter 服务将我重定向到我的回调时,我在 WebView 中收到错误网页。也就是说我没有重定向到我的活动,我仍然停留在 WebView 中。但是如果通过浏览器尝试相同的方式,它就可以工作。那是什么问题?
主要活动:
public class Twitter extends Activity implements OnClickListener {
Button bSignIn;
TextView status;
private OAuthConsumer consumer;
private OAuthProvider provider;
private String url;
final String TAG = getClass().getName();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
bSignIn = (Button) findViewById(R.id.bSignIn);
status = (TextView) findViewById(R.id.tvStatus);
bSignIn.setOnClickListener(this);
}
public void onClick(View v) {
new OAuthWebViewProcess().execute();
}
public class OAuthWebViewProcess extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(Twitter.this, null,
"Connecting, please wait...");
}
protected Void doInBackground(Void... params) {
try {
consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY,
Constants.CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL,
Constants.ACCESS_URL, Constants.AUTHORIZE_URL);
url = provider.retrieveRequestToken(consumer,
Constants.OAUTH_CALLBACK_URL);
} catch (Exception e) {
Log.e(TAG, "Error during OAUth retrieve request token", e);
}
return null;
}
protected void onPostExecute(Void result) {
//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Intent i = new Intent(Twitter.this, TwitterWebView.class);
i.putExtra("url", Uri.parse(url).toString());
startActivityForResult(i, 1);
dialog.dismiss();
}
}
}
Twitter 的 Web 视图:
public class TwitterWebView extends Activity {
String url;
WebView TwitterWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twitterwebview);
Bundle extras = getIntent().getExtras();
url = extras.getString("url");
try {
TwitterWebView = (WebView) findViewById(R.id.wvTwitter);
TwitterWebView.setWebViewClient(new TwitterWebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
TwitterWebView.getSettings().setJavaScriptEnabled(true);
TwitterWebView.getSettings().setDomStorageEnabled(true);
TwitterWebView.getSettings().setSavePassword(false);
TwitterWebView.getSettings().setSaveFormData(false);
TwitterWebView.getSettings().setSupportZoom(false);
TwitterWebView.loadUrl(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wixanz.app.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Twitter"
android:label="@string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TwitterWebView"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<activity
android:name=".TweetList"
android:label="TweetList"
android:launchMode="singleInstance" >
<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="callback"
android:scheme="twitter" />
</intent-filter>
</activity>
</application>
</manifest>