-2

当我尝试在 android 中登录 twitter 应用程序时,它在 logcat 中显示以下错误。我无法通过我的应用程序启动 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

4 回答 4

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);
于 2013-03-18T07:30:05.410 回答
0

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

StrictMode.enableDefaults();

谢谢大家的回复。

于 2013-03-18T09:17:11.370 回答
0

Google 在其最新版本的 android OS 中强制执行网络相关的事情或异步任务上的其他繁重任务..以避免ANR错误

注意日志中的主线程异常原因:android.os.NetworkOnMainThreadException

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

尝试在 ASYNC 任务中运行您的任务意味着在doInBackground.

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

        }
    };
于 2013-03-18T09:20:46.120 回答
0

请参考此链接

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

你从这个链接得到一些想法

并检查是否启用严格模式?如果不启用请启用

StrictMode.enableDefaults();
于 2013-03-18T09:27:33.477 回答