问题在Android Twitter的整合问题、Android、Twitter

由网友(人不风流枉少年)分享简介:我跟随 此链接网站给Twitter Android上的整合。结果但问题是我的使用者密钥和机密有一些问题,因为当我运行我的应用程序它给的对不起!您的应用程序意外停止错误的当我在的logcat 检查它给空指针异常 我的logcat的结果是 11月7日至七日:06:50.962:ERROR / AndroidRunti...

我跟随 此链接网站给Twitter Android上的整合。结果但问题是我的使用者密钥和机密有一些问题,因为当我运行我的应用程序它给的对不起!您的应用程序意外停止错误的当我在的logcat 检查它给空指针异常

我的logcat的结果是

  11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):了java.lang.RuntimeException:无法恢复活动{com.ecs.android.sample.twitter / COM .ecs.android.sample.twitter.AndroidTwitterSample}:显示java.lang.NullPointerException11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.os.Handler.dispatchMessage(Handler.java:99)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):致:显示java.lang.NullPointerException11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在com.ecs.android.sample.twitter.TwitterUtils.isAuthenticated(TwitterUtils.java:18)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在com.ecs.android.sample.twitter.AndroidTwitterSample.updateLoginStatus(AndroidTwitterSample.java:67)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在com.ecs.android.sample.twitter.AndroidTwitterSample.onResume(AndroidTwitterSample.java:63)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.Activity.performResume(Activity.java:3823)11月7日至七日:06:50.962:ERROR / AndroidRuntime(323):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118) 

请告诉我什么是我作出了错误?

twitter推特手机版 twitter客户端下载v6.44.0安卓版

和整个code。与5班是:

常量讲座包com.ecs.android.sample.twitter;

包com.ecs.android.sample.twitter;

 公共类常量{    公共静态最后弦乐CONSUMER_KEY =消费者键;    公共静态最后弦乐CONSUMER_SECRET =消费者密钥    公共静态最后弦乐REQUEST_URL =htt​​p://api.twitter.com/oauth/request_token;    公共静态最后弦乐ACCESS_URL =htt​​p://api.twitter.com/oauth/access_token;    公共静态最后弦乐AUTHORIZE_URL =htt​​p://api.twitter.com/oauth/authorize;    公共静态最后弦乐CALLBACK_SCHEME =X-oauthflow-微博;    公共静态最后弦乐CALLBACK_HOST =回调;    公共静态最后弦乐CALLBACK_URL = CALLBACK_SCHEME +://+ CALLBACK_HOST;} 

AndroidTwitterSample类 -

 公共类AndroidTwitterSample延伸活动{    私人共享preferences preFS;    私人最终处理程序mTwitterHandler =新的处理程序();    私人TextView的loginStatus;    最终的Runnable mUpdateTwitterNotification =新的Runnable(){        公共无效的run(){            Toast.makeText(getBaseContext(),鸣叫送!,Toast.LENGTH_LONG).show();        }    };    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);        这preFS = preferenceManager.getDefaultShared preferences(本)。        loginStatus =(的TextView)findViewById(R.id.login_status);        巴顿鸣叫=(按钮)findViewById(R.id.btn_tweet);        按钮clearCredentials =(按钮)findViewById(R.id.btn_clear_credentials);        tweet.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){                如果(TwitterUtils.isAuthenticated(preFS)){                    sendTweet();                }其他{                    意图I =新意图(getApplicationContext(),prepareRequestTokenActivity.class);                    i.putExtra(tweet_msg,getTweetMsg());                    startActivity(ⅰ);                }            }        });        clearCredentials.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){                clearCredentials();                updateLoginStatus();            }        });    }    @覆盖    保护无效onResume(){        super.onResume();        updateLoginStatus();    }    公共无效updateLoginStatus(){        loginStatus.setText(登录到微博:+ TwitterUtils.isAuthenticated(preFS));    }    私人字符串getTweetMsg(){        返回+新的Date()从Android应用程序推特的toLocaleString()。    }    公共无效sendTweet(){        线程t =新的Thread(){            @覆盖            公共无效的run(){                尝试{                    TwitterUtils.sendTweet(preFS,getTweetMsg());                    mTwitterHandler.post(mUpdateTwitterNotification);                }赶上(例外前){                    ex.printStackTrace();                }            }        };        t.start();    }    私人无效clearCredentials(){        共享preferences preFS = preferenceManager.getDefaultShared preferences(本);        最终的编辑器编辑= prefs.edit();        edit.remove(OAuth.OAUTH_TOKEN);        edit.remove(OAuth.OAUTH_TOKEN_SECRET);        edit.commit();    }} 

Twitter的utils的类 -

 进口oauth.signpost.OAuth;进口twitter4j.Twitter;进口twitter4j.TwitterFactory;进口twitter4j.http.AccessToken;进口android.content.Shared preferences;公共类TwitterUtils {    公共静态布尔isAuthenticated(共享preferences preFS){        字符串标记= prefs.getString(OAuth.OAUTH_TOKEN,);        字符串的秘密= prefs.getString(OAuth.OAUTH_TOKEN_SECRET,);        一个的accessToken =新的accessToken(令牌,密);        叽叽喳喳叽叽喳喳=新TwitterFactory()的getInstance()。        twitter.setOAuthConsumer(Constants.CONSUMER_KEY,Constants.CONSUMER_SECRET);        twitter.setOAuthAccessToken(一);        尝试{            twitter.getAccountSettings();            返回true;        }赶上(例外五){            返回false;        }    }    公共静态无效sendTweet(共享preferences preFS,弦乐味精)抛出异常{        字符串标记= prefs.getString(OAuth.OAUTH_TOKEN,);        字符串的秘密= prefs.getString(OAuth.OAUTH_TOKEN_SECRET,);        一个的accessToken =新的accessToken(令牌,密);        叽叽喳喳叽叽喳喳=新TwitterFactory()的getInstance()。        twitter.setOAuthConsumer(Constants.CONSUMER_KEY,Constants.CONSUMER_SECRET);        twitter.setOAuthAccessToken(一);        twitter.updateStatus(MSG);    }} 

OAuthRequestToken讲座

 公共类OAuthRequestTokenTask扩展的AsyncTask<太虚,太虚,太虚> {    最后的字符串标记=的getClass()的getName()。    私人上下文的背景下;    私人OAuthProvider供应商;    私人消费OAuthConsumer;    公共OAuthRequestTokenTask(上下文的背景下,消费OAuthConsumer,OAuthProvider提供商){        this.context =背景;        this.consumer =消费;        this.provider =供应商;    }    公共OAuthRequestTokenTask(prepareRequestTokenActivity context2,            CommonsHttpOAuthConsumer consumer2,            CommonsHttpOAuthProvider Provider2之间){        // TODO自动生成构造函数存根    }    @覆盖    保护无效doInBackground(虚空...... PARAMS){        尝试{            Log.i(TAG,从谷歌服务器检索请求令牌);            最终字符串URL = provider.retrieveRequestToken(消费者,Constants.CALLBACK_URL);            Log.i(TAG,啪浏览器在authorize URL:+网址);            意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL))setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP。|                        Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);            context.startActivity(意向);        }赶上(例外五){            Log.e(TAG,OAuth的过程中出现错误检索请求令牌,E);        }        返回null;    }} 

prepareRequestTokenActivity讲座

 公共类OAuthRequestTokenTask扩展的AsyncTask<太虚,太虚,太虚> {    最后的字符串标记=的getClass()的getName()。    私人上下文的背景下;    私人OAuthProvider供应商;    私人消费OAuthConsumer;    公共OAuthRequestTokenTask(上下文的背景下,消费OAuthConsumer,OAuthProvider提供商){        this.context =背景;        this.consumer =消费;        this.provider =供应商;    }    公共OAuthRequestTokenTask(prepareRequestTokenActivity context2,            CommonsHttpOAuthConsumer consumer2,            CommonsHttpOAuthProvider Provider2之间){        // TODO自动生成构造函数存根    }    @覆盖    保护无效doInBackground(虚空...... PARAMS){        尝试{            Log.i(TAG,从谷歌服务器检索请求令牌);            最终字符串URL = provider.retrieveRequestToken(消费者,Constants.CALLBACK_URL);            Log.i(TAG,啪浏览器在authorize URL:+网址);            意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL))setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP。|                        Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);            context.startActivity(意向);        }赶上(例外五){            Log.e(TAG,OAuth的过程中出现错误检索请求令牌,E);        }        返回null;    }} 

解决方案

您必须把 CONSUMER_KEY =在这里输入使用者密钥 CONSUMER_SECRET =你的常量班在这里输入消费者秘密。对于你必须注册在Twitter的应用程序。要注册您的应用程序,访问 https://dev.twitter.com/user 。之后,它会给你的钥匙。

I follow this link site to integrate Twitter on Android. But the problem is my consumer key and secret have some problem because when I run my application it gives the Sorry! your application has stopped unexpectedly error and when I checked in logcat it's giving null pointer exception

my logcat result is

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)

Please tell me what is the mistake I am making?

And the whole code with 5 classes is:

Constants class- package com.ecs.android.sample.twitter;

package 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 Class -

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 Utils Class -

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

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

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

解决方案

You have to put CONSUMER_KEY = "enter consumer key here" and CONSUMER_SECRET="enter consumer secret here" in your Constants class. For that you have to register your application in Twitter. To register your app, visit https://dev.twitter.com/user . After that it will give you those keys.

阅读全文

相关推荐

最新文章