从Blackberry发送推文 - 在模拟器中工作,但不在设备上工作

时间:2013-09-20 08:43:58

标签: twitter oauth blackberry

我正在开发Blackberry Twitter集成以在Twitter上发布消息。

该应用程序在模拟器中运行良好,但在我在设备上运行时会出现异常。

以下是我每次在设备上运行应用程序时遇到的异常{

  

OAuth_IO_Exception

我做了一些研究并看到了在设备上设置正确日期时间的解决方案,但它仍然无法正常工作。

这是我的代码:

import com.kc.twitter.activity.TweetToFriend;
import com.twitterapime.rest.Credential;
import com.twitterapime.xauth.Token;
import com.twitterapime.xauth.ui.OAuthDialogListener;
import com.twitterapime.xauth.ui.OAuthDialogWrapper;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class TwitterScreen extends MainScreen  {

    private final String CONSUMER_KEY = "{redected}";
    private final String CONSUMER_SECRET = "{redected}";
    private final String CALLBACK_URL = "http://kingdomconnectng.net/redirect.php"; 
    private LabelField _labelStutus;

    public static OAuthDialogWrapper pageWrapper = null;
    private ShowAuthBrowser showAuthBrowserScreen;
    private String adminMessage;
    public String adminMessages[];

    boolean done=false;

    Credential c ;
    TweetToFriend tw;

    public static StoreToken _tokenValue;

    public static boolean isTweetPosted=false;
    public static boolean isTweeterScreen =false ;

    public TwitterScreen(final String adminMessage)
    {
        this.adminMessage=adminMessage;
        isTweeterScreen = true ;

        showAuthBrowserScreen = new ShowAuthBrowser();

        _tokenValue = StoreToken.fetch();

        /*if(_tokenValue.token.equalsIgnoreCase("nothing"))
        {*/
        showAuthBrowserScreen.doAuth(null);
        UiApplication.getUiApplication().pushScreen(showAuthBrowserScreen);
        /*}
        else
        { 
            final Token t = new Token(_tokenValue.token, _tokenValue.secret, 
                    _tokenValue.userId, _tokenValue.username);
            Credential c = new Credential(CONSUMER_KEY, CONSUMER_SECRET, t);

            TweetToFriend tw=new TweetToFriend();

            boolean done=tw.doTweet(adminMessage, c);
            if(done==true){
                Dialog.alert("Tweet posted.");
                UiApplication.getUiApplication().invokeLater(new Runnable(){
                    public void run(){
                        UiApplication.getUiApplication().popScreen();
                    }
                });
                isTweetPosted=true;
            }
            else{
                Dialog.alert("Tweet not posted.");
                UiApplication.getUiApplication().invokeLater(new Runnable(){
                    public void run(){
                        UiApplication.getUiApplication().popScreen();
                    }
                });
                isTweetPosted=true;
            }
        }*/
    }

    class ShowAuthBrowser extends MainScreen implements OAuthDialogListener
    {
        BrowserField b = new BrowserField();

        public ShowAuthBrowser() 
        {
            //if(isTweetPosted==false){

            UiApplication.getUiApplication().invokeLater(new Runnable(){
                public void run(){
                    UiApplication.getUiApplication().popScreen();
                }
            });
            //}

            _labelStutus = new LabelField("KingdomConnect App" );
            //add(_labelStutus );
            add(b);
            pageWrapper = new BrowserFieldOAuthDialogWrapper(b,CONSUMER_KEY,CONSUMER_SECRET,CALLBACK_URL,this);
            pageWrapper.setOAuthListener(this);

        }
        public void doAuth( String pin )
        {
            try
            {
                if ( pin == null )
                {
                    pageWrapper.login();
                }
                else
                {
                    this.deleteAll();
                    add(b);
                    pageWrapper.login(pin);
                } 

            }
            catch ( Exception e )
            {
                final String message = "Error loggin Twitter: " + e.getMessage();
                Dialog.alert( message );

            }           
        }

        public void onAccessDenied(String response ) {

            System.out.println("Access denied! -> " + response );
            updateScreenLog( "Acceso denegado! -> " + response );

        }

        public void onAuthorize(final Token token) {
            UiApplication.getUiApplication().invokeLater(new Runnable(){
                public void run(){
                    UiApplication.getUiApplication().popScreen();
                }
            });

            final Token myToken = token;
            _tokenValue = StoreToken.fetch();
            _tokenValue.token = myToken.getToken();
            _tokenValue.secret = myToken.getSecret();
            _tokenValue.userId = myToken.getUserId();
            _tokenValue.username = myToken.getUsername();
            _tokenValue.save();

            UiApplication.getUiApplication().invokeLater( new Runnable() {

                public void run() {
                    try{
                        deleteAll();
                        c = new Credential(CONSUMER_KEY,    CONSUMER_SECRET, myToken);
                        tw = new TweetToFriend();

                        /*done=tw.doTweet("KingdomConnect App: "+adminMessage, c);
                            if(done == true)
                            {
                                Dialog.alert( "Buzz posted successfully." );
                                close();
                            }                           
                         */
                        prepareMessage(adminMessage);

                    }catch(NullPointerException e){

                    }catch(Exception e){

                    }
                }
            });

        }

        public void onFail(String arg0, String arg1) {
            updateScreenLog("Error authenticating user! -> " + arg0 + ", " + arg1);
        }
    }


    //TWEET METHOD
    public void tweet(String message){
        done=tw.doTweet("KingdomConnect: "+message, c);
        /*if(done == true){
            Dialog.alert( "Shared successfully." );
            close();
        }   */
    }

    //PREPARE MESSAGE TO TWEET
    public void prepareMessage(String msg){
        int charLimit = 120;
        int _msgSize = msg.length();
        int i=0;
        int parts = _msgSize/124;
        try {
            if(_msgSize > charLimit){
                int temp1=0,temp2=charLimit;
                while(i<parts+1){
                    String data = null;
                    if(i==parts){
                        data = msg.substring(temp1, _msgSize);

                    }else{
                        data = msg.substring(temp1, temp2);
                        temp1=temp2;
                        temp2=temp2+charLimit;
                    }
                    i++;
                    tweet(data);
                }

                    Dialog.alert( "Shared successfully." );
            }else{
                tweet(msg);
                //System.out.println("Data:======================"+msg);
            }
        }catch (Exception e) {
            System.out.println("e = "+e);
        }

    }

    private void updateScreenLog( final String message )
    {
        UiApplication.getUiApplication().invokeLater( new Runnable() {

            public void run() {
                _labelStutus.setText( message );                
            }
        });
    }

}

0 个答案:

没有答案