Android上的XMPP Smack 4.1.0 rc1 ACCES(权限被拒绝)

时间:2015-03-21 22:40:34

标签: android connection xmpp smack asmack

我试图通过https://github.com/igniterealtime/Smack与Smack 4.1.0 rc1进行XMPP连接 我按照本指南https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide导入了Gradle。

源代码:

package com.example.xmpp_app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

import java.io.IOException;

public class MainActivity extends ActionBarActivity {

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

        // Create the configuration for this new connection
        XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
        configBuilder.setUsernameAndPassword("test@example.com", "password123");
        configBuilder.setResource("test");
        configBuilder.setServiceName("127.0.0.1");

        AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
        // Connect to the server
        try {
            connection.connect();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        // Log into the server
        try {
            connection.login();
        } catch (XMPPException e) {
            e.printStackTrace();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Disconnect from the server
        connection.disconnect();
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

摇篮:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

build gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.xmpp_app"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile "org.igniterealtime.smack:smack-android:4.1.0-rc1"
    // Optional for XMPPTCPConnection
    compile "org.igniterealtime.smack:smack-tcp:4.1.0-rc1"
    // Optional for XMPP-IM (RFC 6121) support (Roster, Threaded Chats, …)
    compile "org.igniterealtime.smack:smack-im:4.1.0-rc1"
    // Optional for XMPP extensions support
    compile "org.igniterealtime.smack:smack-extensions:4.1.0-rc1"
}

ERROR:

我找到了一些如何申请互联网许可的指南,但我现在不能在家测试它,我怎么能用我的本地主机试试呢?当我尝试这个时它会说

Exception while resovling SRV records for 127.0.0.1. Consider adding '_xmpp-(server|client)._tcp' DNS SRV Records android.os.NetworkOnMainThreadException 

即使它仍然说出与前一个相同的错误消息

03-21 14:57:30.332 1176-1176/com.example.xmpp_app W/System.err﹕ org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '127.0.0.1:5222' failed because java.net.SocketException: socket failed: EACCES (Permission denied) 03-21 14:57:30.372 1176-1176/com.example.xmpp_app W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPCo‌​nnection.java:574

有人可以帮我解决这个问题吗?我只是想检查连接是否有效。

注意:我已添加:<uses-permission android:name="android.permission.INTERNET" /> 在我的机器人清单上。

3 个答案:

答案 0 :(得分:4)

您的问题是您在主线程中发出网络请求 并且此网络电话会引发android.os.NetworkOnMainThreadException 你必须使用AsyncTask或java中的简单线程创建一个单独的线程,然后调用smack进行连接,登录并执行与服务器有关的任何操作。

http://developer.android.com/reference/android/os/AsyncTask.html

答案 1 :(得分:0)

您使用的是Android模拟器吗?如果你是android模拟器,你应该使用10.0.2.2作为localhost。 127.0.0.1是它自己的环回接口。 还要添加INTERNET权限

请参阅http://developer.android.com/tools/devices/emulator.html

答案 2 :(得分:0)

连接必须在后台线程中,像这样。

 AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    getMainActivity().connection.connect().login();
                } catch (XMPPException e) {
                    e.printStackTrace();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Log.d(TAG, "onCreate: isConnected " + getMainActivity().connection.isConnected());
                Log.d(TAG, "onCreate: isAuthenticated " + getMainActivity().connection.isAuthenticated());
            }
        });