如何解决无法解析“api.twitter.com”没有与主机名关联的地址

时间:2013-10-30 18:02:37

标签: android twitter twitter4j fatal-error

当我尝试在Twitter上分享帖子时,我得到了这个错误 我将twitter4j用于库的Twitter类是:

public class TwitterActivity extends Activity {

    private Twitter twitter;

    private RequestToken requestToken = null;
    public static final String PREFS = "MyPrefsFile";
    final public static String CALLBACK_URL = "app://casa";
    private SharedPreferences shared;
    private RequestToken re;
    private int it = 0;
    private String frase = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twitter);

        it = getIntent().getExtras().getInt("punteggio");
        frase = getIntent().getExtras().getString("frase");
        shared = this.getSharedPreferences(PREFS, Context.MODE_PRIVATE);

        new updateTwitterStatus().execute();

    }

    @Override
    public void onNewIntent(Intent data) {
        super.onNewIntent(data);
        dealWithTwitterResponse(data);

    }

    class updateTwitterStatus extends AsyncTask<Void, Void, String> {

        private Intent i = null;

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

            ConfigurationBuilder cb = new ConfigurationBuilder();

            cb.setDebugEnabled(true)
                    .setOAuthConsumerKey("V***********")
                    .setOAuthConsumerSecret(
                            "****************");

            TwitterFactory tf = new TwitterFactory(cb.build());
            twitter = tf.getInstance();

            try {
// the next line throws the error
                requestToken = twitter.getOAuthRequestToken(CALLBACK_URL);
                Log.i("bauu", "miao");

                String authUrl = requestToken.getAuthenticationURL();

                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(requestToken.getAuthenticationURL())));

                return authUrl;

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void dealWithTwitterResponse(Intent intent) {
        Log.i("Sonod entro", "vau");
        final Uri uri = intent.getData();
        if (uri == null) {
            Log.i("è null", "null");
        }
        Log.i("callback funziona", "ciao");
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
            final String verifier = uri.getQueryParameter("oauth_verifier");
            final String oauthToken = uri.getQueryParameter("oauth_token");
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Log.i("SOno dentro il run", "asd");
                        AccessToken at = twitter.getOAuthAccessToken(
                                requestToken, verifier);

                        twitter.setOAuthAccessToken(at);
                        twitter.updateStatus("CHI VUOLE ESSERE SCIENZIATO?? Punteggio: "
                                + it
                                + " "
                                + frase
                                + " "
                                + "www.scienze-naturali.com");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.punteggio, menu);
        return true;
    }

}

和xml FIle是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.applicazionescienza"
    android:versionCode="1"
    android:versionName="1.0" >



    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <uses-permission
    android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.applicazionescienza.MenuPrincipale"
            android:label="@string/app_name"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.applicazionescienza.Informazioni"
            android:label="@string/title_activity_informazioni" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Regolamento"
            android:label="@string/title_activity_regolamento" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Gioca"
            android:label="@string/title_activity_gioca" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Livello"
            android:label="@string/title_activity_livello" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Punteggio"
            android:label="@string/title_activity_punteggio" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.TwitterActivity"
            android:label="@string/title_activity_twitter"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="casa"
                    android:scheme="app" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.applicazionescienza.FacebookActivity"
            android:label="@string/title_activity_facebook" >
        </activity>
    </application>

</manifest>

我在代码中签署了抛出错误的行 有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:2)

我在平板电脑上遇到了同样的错误。我似乎在设备上连接到互联网,但实际上存在连接问题。要修复错误,我必须在设备上禁用并重新启用wifi。

答案 1 :(得分:0)

在同一设备上,尝试在浏览器中转到https://api.twitter.com。如果它连接起来,那么它最有可能是app / twitter4j的问题而不是wi-fi。