2
votes

I'm trying to do an XMPP-Connection with Smack 4.1.0 rc1 from https://github.com/igniterealtime/Smack I followed this guide https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide importing the Gradle.

Source code:

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("[email protected]", "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);
    }
}

Gradle:

// 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:

I found some guides how to request internet permission but i can not test it coz im not at home now, how can i try it with my localhost? when i try this it says

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

Even though it's still saying the same error message than the previous one

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

Anyone could help me out with this problem please?, I am just trying to check if the connection works.

Note: I have already added: <uses-permission android:name="android.permission.INTERNET" /> on my android manifest.

3
possible duplicate of android.os.NetworkOnMainThreadExceptionFlow
Now this is the next error: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.kobbycoder

3 Answers

4
votes

Your problem is that you are making a network request in your Main Thread and this network call throws a android.os.NetworkOnMainThreadException You have to create a separate Thread using a AsyncTask or a simple Thread in java, and then, call smack to connect, login and do whatever you have to do with your server.

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

0
votes

Are you using android emulator? If you are android emulator you should use 10.0.2.2 as a localhost. 127.0.0.1 is its own loopback interface. Also add INTERNET permission

Please see http://developer.android.com/tools/devices/emulator.html

0
votes

The connection must be in Background thread, Like this.

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