我正嘗試通過mongolabs在我正在開發的android應用程序中連接到mongodb,但它沒有連接,而是拋出異常(或在至少我認爲是)。我不熟悉後端,所以如果我犯了一個致命的菜鳥的錯誤,請原諒我。這是logcat使用REST接口連接到數據庫:無法連接
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):at 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類的部分我寫來訪問數據庫並獲取項目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");
}
,這裏是部分它在
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);