gpt4 book ai didi

android - Android 上的 Twitter 集成问题

转载 作者:搜寻专家 更新时间:2023-11-01 09:13:20 24 4
gpt4 key购买 nike

我关注 这个link在 Android 上集成 Twitter 的网站。
但问题是我的消费者 key 和 secret 有一些问题,因为当我运行我的应用程序时,它给出了对不起!你的应用程序意外停止错误当我 checkin logcat 时,它给出了 null pointer exception

我的 logcat 结果是

07-07 11:06:50.962: ERROR/AndroidRuntime(323): java.lang.RuntimeException: Unable to resume activity {com.ecs.android.sample.twitter/com.ecs.android.sample.twitter.AndroidTwitterSample}: java.lang.NullPointerException
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): Caused by: java.lang.NullPointerException
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at com.ecs.android.sample.twitter.TwitterUtils.isAuthenticated(TwitterUtils.java:18)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at com.ecs.android.sample.twitter.AndroidTwitterSample.updateLoginStatus(AndroidTwitterSample.java:67)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at com.ecs.android.sample.twitter.AndroidTwitterSample.onResume(AndroidTwitterSample.java:63)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.Activity.performResume(Activity.java:3823)
07-07 11:06:50.962: ERROR/AndroidRuntime(323): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)

请告诉我我犯了什么错误?

5 个类的完整代码是:

常量类-包 com.ecs.android.sample.twitter;

包 com.ecs.android.sample.twitter;

public class Constants {

public static final String CONSUMER_KEY = "consumer key";
public static final String CONSUMER_SECRET= "consumer secret key";

public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";

public static final String CALLBACK_SCHEME = "x-oauthflow-twitter";
public static final String CALLBACK_HOST = "callback";
public static final String CALLBACK_URL = CALLBACK_SCHEME + "://" + CALLBACK_HOST;
}

AndroidTwitterSample 类 -

public class AndroidTwitterSample extends Activity {

private SharedPreferences prefs;
private final Handler mTwitterHandler = new Handler();
private TextView loginStatus;

final Runnable mUpdateTwitterNotification = new Runnable() {
public void run() {
Toast.makeText(getBaseContext(), "Tweet sent !", Toast.LENGTH_LONG).show();
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);

loginStatus = (TextView)findViewById(R.id.login_status);
Button tweet = (Button) findViewById(R.id.btn_tweet);
Button clearCredentials = (Button) findViewById(R.id.btn_clear_credentials);

tweet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (TwitterUtils.isAuthenticated(prefs)) {
sendTweet();
} else {
Intent i = new Intent(getApplicationContext(), PrepareRequestTokenActivity.class);
i.putExtra("tweet_msg",getTweetMsg());
startActivity(i);
}
}
});

clearCredentials.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clearCredentials();
updateLoginStatus();
}
});
}

@Override
protected void onResume() {
super.onResume();
updateLoginStatus();
}

public void updateLoginStatus() {
loginStatus.setText("Logged into Twitter : " + TwitterUtils.isAuthenticated(prefs));
}


private String getTweetMsg() {
return "Tweeting from Android App at " + new Date().toLocaleString();
}

public void sendTweet() {
Thread t = new Thread() {
@Override
public void run() {

try {
TwitterUtils.sendTweet(prefs,getTweetMsg());
mTwitterHandler.post(mUpdateTwitterNotification);
} catch (Exception ex) {
ex.printStackTrace();
}
}

};
t.start();
}

private void clearCredentials() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final Editor edit = prefs.edit();
edit.remove(OAuth.OAUTH_TOKEN);
edit.remove(OAuth.OAUTH_TOKEN_SECRET);
edit.commit();
}
}

Twitter 实用程序类 -

import oauth.signpost.OAuth;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import android.content.SharedPreferences;

public class TwitterUtils {

public static boolean isAuthenticated(SharedPreferences prefs) {

String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);

try {
twitter.getAccountSettings();
return true;
} catch (Exception e) {
return false;
}
}

public static void sendTweet(SharedPreferences prefs,String msg) throws Exception {
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
twitter.updateStatus(msg);
}
}

OAuthRequestToken 类-

public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> {

final String TAG = getClass().getName();
private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;


public OAuthRequestTokenTask(Context context,OAuthConsumer consumer,OAuthProvider provider) {
this.context = context;
this.consumer = consumer;
this.provider = provider;
}

public OAuthRequestTokenTask(PrepareRequestTokenActivity context2,
CommonsHttpOAuthConsumer consumer2,
CommonsHttpOAuthProvider provider2) {
// TODO Auto-generated constructor stub
}


@Override
protected Void doInBackground(Void... params) {

try {
Log.i(TAG, "Retrieving request token from Google servers");
final String url = provider.retrieveRequestToken(consumer, Constants.CALLBACK_URL);
Log.i(TAG, "Popping a browser with the authorize URL : " + url);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "Error during OAUth retrieve request token", e);
}

return null;
}
}

PrepareRequestTokenActivity 类-

public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> {

final String TAG = getClass().getName();
private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;

public OAuthRequestTokenTask(Context context,OAuthConsumer consumer,OAuthProvider provider) {
this.context = context;
this.consumer = consumer;
this.provider = provider;
}

public OAuthRequestTokenTask(PrepareRequestTokenActivity context2,
CommonsHttpOAuthConsumer consumer2,
CommonsHttpOAuthProvider provider2) {
// TODO Auto-generated constructor stub
}


@Override
protected Void doInBackground(Void... params) {

try {
Log.i(TAG, "Retrieving request token from Google servers");
final String url = provider.retrieveRequestToken(consumer, Constants.CALLBACK_URL);
Log.i(TAG, "Popping a browser with the authorize URL : " + url);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "Error during OAUth retrieve request token", e);
}

return null;
}
}

最佳答案

您必须将 CONSUMER_KEY = "enter consumer key here"CONSUMER_SECRET="enter consumer secret here" 放在您的 Constants 类中。为此,您必须在 Twitter 中注册您的应用程序。要注册您的应用程序,请访问 https://dev.twitter.com/user .之后它将为您提供这些 key 。

关于android - Android 上的 Twitter 集成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6606678/

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