使用 REST 接口连接到数据库:无法连接
我正在尝试通过我正在开发的android应用程序中的mongolabs REST接口连接到mongodb,但它没有连接,而是抛出异常(或者至少我认为是)。我对后端不熟悉,所以如果我犯了一个致命的菜鸟错误,请原谅我。这是日志猫
01-10 16:28:50.377: W/System.err(630): javax.net.ssl.SSLException: 证书中的主机名不匹配: != OR OR >01-10 16:28: 50.377:W/System.err(630):位于org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:185) 01-10>16:28:50.388: W/System.err(630): 在 org.apache.http.conn.ssl .BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
下面是部分我编写的 MongoLabHelper 类用于访问数据库并获取名称等项目
HttpClient client;
JSONObject db;
MongoLabHelper() throws ClientProtocolException, IOException, JSONException{
client = new DefaultHttpClient();
HttpGet request = new HttpGet("https://api.mongolab.com/api/1/databases/breadcrumbs/collections/crumbs?apiKey=xxxxxxxxxxxxx");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
String json = in.toString();
db = new JSONObject(json);
}
public String getName(String name) throws JSONException {
JSONObject doc = db.getJSONObject(name);
return doc.getString("name");
}
,这是它所使用的类的一部分
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String name = "Crumb Not Available";
MongoLabHelper help;
try {
help = new MongoLabHelper();
name = help.getName("Chipotle");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(R.layout.breadcrumb);
TextView crumbName = (TextView) findViewById(R.id.crumb_name);
crumbName.setText(name);
I am trying to connect to a mongodb through mongolabs REST interface in the android app I'm developing, but it is not connecting, and instead it is throwing an exception (or at least i think it is). I am not familiar with backends, so if I am making a fatal rookie's mistake, please forgive me. This is the logcat
01-10 16:28:50.377: W/System.err(630): javax.net.ssl.SSLException: hostname in certificate didn't match: != OR OR >01-10 16:28:50.377: W/System.err(630): at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:185) 01-10 >16:28:50.388: W/System.err(630): at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
Below is the part of MongoLabHelper class i wrote to access the database and get items like names
HttpClient client;
JSONObject db;
MongoLabHelper() throws ClientProtocolException, IOException, JSONException{
client = new DefaultHttpClient();
HttpGet request = new HttpGet("https://api.mongolab.com/api/1/databases/breadcrumbs/collections/crumbs?apiKey=xxxxxxxxxxxxx");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
String json = in.toString();
db = new JSONObject(json);
}
public String getName(String name) throws JSONException {
JSONObject doc = db.getJSONObject(name);
return doc.getString("name");
}
and here is part the class it is used in
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String name = "Crumb Not Available";
MongoLabHelper help;
try {
help = new MongoLabHelper();
name = help.getName("Chipotle");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(R.layout.breadcrumb);
TextView crumbName = (TextView) findViewById(R.id.crumb_name);
crumbName.setText(name);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您需要显式设置 HttpClient 来处理 SSL。我相信这个 stackoverflow 线程包含您需要的信息:
Secure HTTP Post in Android
I'为了方便起见,我们将从线程中复制相关的代码:
You actually need to explicitly setup the HttpClient to handle SSL. I believe this stackoverflow thread has the information you need:
Secure HTTP Post in Android
I'll copy the relevant bit of code from the thread for convenience:
作为帮手;
我还实现了 Android 库来抽象 MongoLab 通信。
主要目标是创建一个易于使用的库,直接从 Android 应用程序利用云中 Mongo 的强大功能!
注意:我还添加了使用 MongoLab 的自定义 ACRA 报告器。
这是第一个版本(我将继续扩展):
-> https://github.com/wareninja/mongolab-sdk
As a helper;
I've also implemented Android library to abstract MongoLab communication.
The main goal is to make an easy to use a library that leverages the power of Mongo in the cloud, directly from Android applications!
Note: I've also included custom ACRA reporter that uses MongoLab.
here is the first version (I'll keep extending):
-> https://github.com/wareninja/mongolab-sdk
您有一个来自应用程序 http://lolapriego.com/blog/?p=16
或者你可以看看这个要点,你可以在其中看到如何在 Android 中发出 http 请求
}
You have an example here from an application http://lolapriego.com/blog/?p=16
or you can take a look at this Gist where you can see how to make http request in Android
}