gpt4 book ai didi

java - 无法从网络服务连接到 mongodb

转载 作者:行者123 更新时间:2023-11-28 22:29:33 25 4
gpt4 key购买 nike

我创建了一个 Android 应用程序,它向我的 Tomcat 网络服务器发送 SOAP 请求。Web 服务器有一个调用另一个类 (DataLayer.java) 的 Web 服务 (MongoService.wsdl),该类具有一个静态方法 (getConnection),该方法尝试初始化 MongClient 类(新 MongoClient),但失败且未抛出任何异常。

当我从 Java 应用程序运行此函数时,它运行良好。

方法:

/**Utility to Get the Connection from the database*/
public static DBCollection getConnection(String dbName, String collectionName)throws UnknownHostException {

MongoClient mongo = null;
try {
mongo = new MongoClient( "localhost" , 27017 );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**Gets database, incase if the database is not existing
MongoDB Creates it for you*/
DB db = mongo.getDB(dbName);

/**Gets collection / table from database specified if
collection doesn't exists, MongoDB will create it for
you*/
DBCollection table = db.getCollection(collectionName);
return table;
}

日志:

09-24 23:23:14.223: W/System.err(18551): SoapFault - faultcode: 'soapenv:Server.userException' faultstring: 'java.lang.reflect.InvocationTargetException' faultactor: 'null' detail: org.kxml2.kdom.Node@42d023c8 09-24 23:23:14.223: W/System.err(18551): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:112) 09-24 23:23:14.223: W/System.err(18551): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137) 09-24 23:23:14.223: W/System.err(18551): at org.ksoap2.transport.Transport.parseResponse(Transport.java:63) 09-24 23:23:14.223: W/System.err(18551): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100) 09-24 23:23:14.223: W/System.err(18551): at com.example.myfirstapp.WebService.invokeHelloWorldWS(WebService.java:51) 09-24 23:23:14.223: W/System.err(18551): at com.example.myfirstapp.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:251) 09-24 23:23:14.223: W/System.err(18551): at com.example.myfirstapp.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1) 09-24 23:23:14.223: W/System.err(18551): at android.os.AsyncTask$2.call(AsyncTask.java:288) 09-24 23:23:14.223: W/System.err(18551): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 09-24 23:23:14.223: W/System.err(18551): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 09-24 23:23:14.223: W/System.err(18551): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 09-24 23:23:14.223: W/System.err(18551): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 09-24 23:23:14.223: W/System.err(18551): at java.lang.Thread.run(Thread.java:841) 09-24 23:23:14.233: I/dalvikvm(18551): Jit: resizing JitTable from 4096 to 8192 09-24 23:23:55.967: D/dalvikvm(18551): GC_FOR_ALLOC freed 3538K, 19% free 16265K/20064K, paused 49ms, total 49ms

最佳答案

我通过将 mongo-driver.jar 添加到 WEB-INF/lib 文件夹解决了这个问题。如果使用eclipse和maven,在项目属性中的“Deployment Assembly”添加maven依赖即可解决。

关于java - 无法从网络服务连接到 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26025927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com