好吧,我正在尝试让 Twitter 在一个基本的 Android 应用程序上运行,它在我的 2.3.6 中运行良好,但是当我尝试使用 4.0+ 时,它无法尝试进行身份验证。这是我的小代码
private OAuthProvider provider = new DefaultOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL);
private CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setVisibility(View.VISIBLE);
setContentView(webview);
try {
String authUrl = provider.retrieveRequestToken(consumer, OAUTH_CALLBACK_URL);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
Uri uri = Uri.parse(url);
if (uri != null && uri.toString().startsWith(OAUTH_CALLBACK_URL)) {
view.setVisibility(View.INVISIBLE);
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
provider.retrieveAccessToken(consumer, verifier);
String claveUsuario = consumer.getToken();
String secretoUsuario = consumer.getTokenSecret();
HttpParameters params1 = provider.getResponseParameters();
String userName = params1.getFirst("screen_name");
SharedPreferences settings = getBaseContext().getSharedPreferences(PREFERENCIAS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("clave_usuario", claveUsuario);
editor.putString("secreto_usuario", secretoUsuario);
editor.putString("nombre_usuario", userName);
editor.commit();
Intent intent = getIntent();
String origen = intent.getExtras().getString("Origen");
if (origen != null) {
if (origen.equals("Twittear")) {
startActivity(new Intent(AutenticarTwitter.this, Twittear.class));
finish();
}
else if (origen.equals("Autores")) {
startActivity(new Intent(AutenticarTwitter.this, Autores.class));
finish();
}
}
else {
startActivity(new Intent(AutenticarTwitter.this, Ajustes.class));
finish();
}
}
catch (Exception e) {}
}
}
});
webview.loadUrl(authUrl);
}
catch (Exception ex) {}
}
正如我所说,它在 Android 2.3.6 中运行良好,但在 4.0+ 中它失败了:
String authUrl = provider.retrieveRequestToken(consumer, OAUTH_CALLBACK_URL);
我没有成功地尝试解决这个问题。我的 logcat 是这样的:
04-29 22:37:39.256: W/System.err(784): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null
04-29 22:37:39.256: W/System.err(784): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
04-29 22:37:39.256: W/System.err(784): at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
04-29 22:37:39.256: W/System.err(784): at com.example.twitter.OAuthAccessTokenActivity.onResume(OAuthAccessTokenActivity.java:43)
04-29 22:37:39.256: W/System.err(784): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-29 22:37:39.276: W/System.err(784): at android.app.Activity.performResume(Activity.java:5182)
04-29 22:37:39.276: W/System.err(784): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-29 22:37:39.276: W/System.err(784): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-29 22:37:39.276: W/System.err(784): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-29 22:37:39.296: W/System.err(784): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-29 22:37:39.296: W/System.err(784): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-29 22:37:39.296: W/System.err(784): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 22:37:39.296: W/System.err(784): at android.os.Looper.loop(Looper.java:137)