gpt4 book ai didi

Android:WebView 中的 Twitter - 通过回调返回 Activity

转载 作者:行者123 更新时间:2023-11-29 22:07:32 25 4
gpt4 key购买 nike

我尝试创建 Twitter 客户端,现在我通过 OAuth 协议(protocol)处理授权。我已经创建了“登录”按钮以进入 WebView 并加载 Twitter 授权 URL,这是可行的。但是,当授权被成功接受并且 Twitter 服务将我重定向到我的回调时,我在 WebView 中收到错误网页。也就是说我没有重定向到我的 Activity ,我仍然停留在 WebView 中。但如果通过浏览器尝试相同的方式,它就可以工作。这是什么问题?

主要 Activity :

    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 的 WebView:

 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();
}
}
}

list :

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

最佳答案

我对 LinkedIn、Foursquare 等其他网络也做了同样的事情。但是我没有使用回调 URL,而是重写了方法 shouldOverrideUrlLoading (WebView view, String url)在您的 WebViewClient(用于显示登录页面)中自己捕获访问 token 和 token secret (如果需要)。

关于Android:WebView 中的 Twitter - 通过回调返回 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10462721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com