Twitter登录android无法正常工作

时间:2013-03-18 07:27:10

标签: android twitter oauth

当我尝试在android中登录twitter应用程序时,它会在logcat.i中显示以下错误。我无法通过我的应用程序启动twitter。请给我一些解决方案。

03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639): Error creating consumer / provider
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at com.android.twitter.PrepareRequestTokenActivity.initData(PrepareRequestTokenActivity.java:129)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at com.android.twitter.PrepareRequestTokenActivity.onCreate(PrepareRequestTokenActivity.java:64)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.app.Activity.performCreate(Activity.java:4470)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.os.Looper.loop(Looper.java:137)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.app.ActivityThread.main(ActivityThread.java:4511)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at java.lang.reflect.Method.invoke(Method.java:511)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at dalvik.system.NativeStart.main(Native Method)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639): Caused by: android.os.NetworkOnMainThreadException
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at oauth.signpost.commonshttp.CommonsHttpOAuthProvider.sendRequest(CommonsHttpOAuthProvider.java:64)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:177)
03-18 12:47:28.412: E/com.android.twitter.PrepareRequestTokenActivity(4639):    ... 17 more

4 个答案:

答案 0 :(得分:0)

当应用程序尝试在其主线程上执行网络操作时,抛出此异常。在AsyncTask中运行代码:

class RetreiveFeedTask extends AsyncTask<String, Void, RSSFeed> {

    private Exception exception;

    protected RSSFeed doInBackground(String... urls) {
        try {
           //POST YOUR CODE HERE!!
        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(RSSFeed feed) {
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
 }

 new RetreiveFeedTask().execute(urlToRssFeed);

答案 1 :(得分:0)

我只是在我的代码中添加一行来解决这个问题。

  

StrictMode.enableDefaults();

感谢所有回复。

答案 2 :(得分:0)

Google在其最新版本的Android操作系统中强制要求在Asynch Task上执行与网络相关的事情或其他繁重任务。以避免ANR错误

注意主线程异常 在您的日志中引起:android.os.NetworkOnMainThreadException

这个例外是因为如果这个限制..

尝试在ASYNC任务中运行您的任务意味着在doInBackground中调用TWITTER实例。

public class LoginTwitter extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.


            // call your loginTwiter here (the way you are calling twitter AOUTH)

            return false;

        }

        @Override
        protected void onPostExecute(final Boolean success) {


        }


    }

在某些UI上调用此类,实际上您当前正在调用TwitterLogin

  private View.OnClickListener  LoginTwitter=new View.OnClickListener() {

        @Override
        public void onClick(View paramView) {
            // TODO Auto-generated method stub
            new LoginTwitter().execute((Void) null);;

        }
    };

答案 3 :(得分:0)

请参阅此链接

http://androidcodeexamples.blogspot.com/2011/12/how-to-integrate-twitter-in-android.html

您可以从此链接获得一些想法

并检查严格模式启用与否?如果未启用,则启用plz

StrictMode.enableDefaults();