Android连接MongoDB:java.lang.NoClassDefFoundError无法解决以下问题:Ljavax / naming / directory / InitialDirContext;

时间:2018-08-01 10:03:48

标签: java android mongodb

我正在Android Studio上开发自己的应用程序,并且整周无法解决以下所示的异常(我浏览了所有网站,但没有一个网站可以解决我的问题。

我有2个活动:

MainActivity.java

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

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

    }
    public void toNextActivity(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }
    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

SecondActivity.java

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.List;
public class SecondActivity extends AppCompatActivity 
{
    TextView trendText1;
    TextView trendText2;
    TextView trendText3;
    TextView trendText4;
    TextView trendText5;
    TextView currentTrend;
    private String databaseName = "xxxx";
    private String mongodbUsername = "xxxx";
    private String mongodbPassword = "xxxx";
    private String clusterName = "xxxx";
    public class getTrendFromDB extends AsyncTask<Void, Integer, Boolean> {
        private String databaseName;
        private String mongodbUsername;
        private String mongodbPassword;
        private String clusterName;
        private List <String> keywordList;
        protected getTrendFromDB(String db, String usr, String pwd, String cls) {
            this.databaseName = db;
            this.mongodbUsername = usr;
            this.mongodbPassword = pwd;
            this.clusterName = cls;
        }


        protected void onPreExecute() {
            currentTrend.setText("Loading trend...");
        }

        protected Boolean doInBackground(Void... params) {
            MongoClientURI uri = new MongoClientURI(
                    "mongodb+srv://" + this.mongodbUsername + ":" + this.mongodbPassword + "@" + this.clusterName
            );
            MongoClient mongoClient = new MongoClient(uri);
            MongoDatabase dtsDatabase = mongoClient.getDatabase(this.databaseName);
            MongoCollection<Document> keywordCollection = dtsDatabase.getCollection("trend_keywords");
            MongoCursor<Document> cur = keywordCollection.find().iterator();
            Document doc = cur.next();
            this.keywordList = (List<String>) doc.get("list");
            mongoClient.close();
            return true;
        }

        protected void onProgressUpdate(Integer... values){
            trendText1.setText(this.keywordList.get(0));
            trendText2.setText(this.keywordList.get(1));
            trendText3.setText(this.keywordList.get(2));
            trendText4.setText(this.keywordList.get(3));
            trendText5.setText(this.keywordList.get(4));
        }

        protected void onPostExecute(Boolean result) {
            currentTrend.setText("finished!");
        }

    }
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        currentTrend = (TextView) findViewById(R.id.current_trend_text);
        trendText1 = (TextView) findViewById(R.id.trendName1);
        trendText2 = (TextView) findViewById(R.id.trendName2);
        trendText3 = (TextView) findViewById(R.id.trendName3);
        trendText4 = (TextView) findViewById(R.id.trendName4);
        trendText5 = (TextView) findViewById(R.id.trendName5);

        getTrendFromDB task1 = new getTrendFromDB(databaseName, mongodbUsername, mongodbPassword, clusterName);
        task1.execute();


    }
}

从MainActivity切换到第二个时,出现以下异常:

08-01 17:59:49.846 18046-18079/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: io.datats.dts_android.app, PID: 18046
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:354)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/naming/directory/InitialDirContext;
        at com.mongodb.internal.dns.DnsResolver.createDnsDirContext(DnsResolver.java:140)
        at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:58)
        at com.mongodb.ConnectionString.<init>(ConnectionString.java:325)
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:238)
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:220)
        at io.datats.dts_android.forbole_dts.SecondActivity$getTrendFromDB.doInBackground(SecondActivity.java:46)
        at io.datats.dts_android.forbole_dts.SecondActivity$getTrendFromDB.doInBackground(SecondActivity.java:27)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.naming.directory.InitialDirContext" on path: DexPathList[[zip file "/data/app/io.datats.dts_android.app-g-9eU2fEIhL3iFJd3f_DWw==/base.apk", zip file "/data/app/io.datats.dts_android.app-g-9eU2fEIhL3iFJd3f_DWw==/split_forbole_dts.apk"],nativeLibraryDirectories=[/data/app/io.datats.dts_android.app-g-9eU2fEIhL3iFJd3f_DWw==/lib/x86, /data/app/io.datats.dts_android.app-g-9eU2fEIhL3iFJd3f_DWw==/base.apk!/lib/x86, /data/app/io.datats.dts_android.app-g-9eU2fEIhL3iFJd3f_DWw==/split_forbole_dts.apk!/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.mongodb.internal.dns.DnsResolver.createDnsDirContext(DnsResolver.java:140) 
        at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:58) 
        at com.mongodb.ConnectionString.<init>(ConnectionString.java:325) 
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:238) 
        at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:220) 
        at io.datats.dts_android.forbole_dts.SecondActivity$getTrendFromDB.doInBackground(SecondActivity.java:46) 
        at io.datats.dts_android.forbole_dts.SecondActivity$getTrendFromDB.doInBackground(SecondActivity.java:27) 
        at android.os.AsyncTask$2.call(AsyncTask.java:333) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
08-01 17:59:50.299 3661-3752/? E/InputDispatcher: channel 'c7cff48 io.datats.dts_android.app/io.datats.dts_android.forbole_dts.SecondActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-01 17:59:50.327 3661-3752/? E/InputDispatcher: channel '178d1a6 io.datats.dts_android.app/io.datats.dts_android.forbole_dts.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-01 17:59:52.693 1667-1735/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-01 17:59:52.812 1667-1735/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-01 17:59:58.052 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.067 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.072 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.075 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.078 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.084 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.088 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.093 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.109 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.125 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.129 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.135 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.138 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.178 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.190 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.220 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.233 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.238 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.252 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.277 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.289 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.303 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.313 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.315 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.319 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.325 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.326 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.330 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.339 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.348 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.352 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.354 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.360 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.380 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.406 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.413 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.416 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.435 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.469 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.510 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.520 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.530 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.542 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.605 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.610 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.629 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.631 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.632 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.637 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.643 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.652 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.660 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.662 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.670 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.679 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.689 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.710 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.728 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.756 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.759 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.764 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.767 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.768 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.834 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.854 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.859 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.859 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.887 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.972 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:58.986 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.084 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.164 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.262 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.302 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.350 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.384 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.417 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.453 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.478 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.516 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.603 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.663 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.693 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.713 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.760 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.815 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.890 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 17:59:59.958 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 18:00:00.030 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 18:00:00.085 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 18:00:08.303 3661-3680/? E/memtrack: Couldn't load memtrack module
08-01 18:00:17.691 3661-8661/? E/memtrack: Couldn't load memtrack module
08-01 18:00:19.284 1680-1756/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
08-01 18:00:22.981 3661-3685/? E/BluetoothAdapter: Bluetooth binder is null
08-01 18:00:22.991 3661-3685/? E/KernelCpuSpeedReader: Failed to read cpu-freq: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state (No such file or directory)

我已尝试根据搜索到的信息更改依赖关系,但失败了。这是我的build.gradle

build.gradle

apply plugin: 'com.android.feature'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets', 'src/main/assets/']
            resources.srcDirs =['src/main/resources', 'src/main/resources/']
} }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(':base')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compile 'org.mongodb:mongo-java-driver:3.8.0'
    compile "org.igniterealtime.smack:smack-android-extensions:4.1.8"
    compile "org.igniterealtime.smack:smack-experimental:4.1.8"
    compile "org.igniterealtime.smack:smack-tcp:4.1.8"
}

顺便说一句,用于SecondActivity.java的对MongoClientURI和MongoClient代码的相同请求在Eclipse Java上运行良好。我猜问题出在Android Studio包含的Java包中。谁能帮助我解决这个奇怪的问题?

0 个答案:

没有答案