2012-01-10 216 views
2

我正嘗試通過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); 

回答

3

使用該類實際上,你需要明確設置HttpClient的處理SSL。我相信這個計算器線程有你需要的信息:

Secure HTTP Post in Android

我會從線程的代碼中的相關位複製的方便:

private HttpClient createHttpClient() 
{ 
    HttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
    HttpProtocolParams.setUseExpectContinue(params, true); 

    SchemeRegistry schReg = new SchemeRegistry(); 
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); 

    return new DefaultHttpClient(conMgr, params); 
} 
0

作爲幫手;
我也實現了Android庫來抽象MongoLab通信。
主要目標是使直接從Android應用程序直接利用雲中Mongo功能的庫變得容易!
注意:我還包括使用MongoLab的自定義ACRA記者。

這裏是第一個版本(我會繼續擴大)
- >https://github.com/wareninja/mongolab-sdk

0

你必須從一個應用程序http://lolapriego.com/blog/?p=16

,或者你可以看看這個要點這裏的例子在這裏你可以看到如何進行HTTP請求的Android

public class CustomHttpClient { 
/** The time it takes for our client to timeout */ 
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds 

/** Single instance of our HttpClient */ 
private static HttpClient mHttpClient; 

/** 
* Get our single instance of our HttpClient object. 
* 
* @return an HttpClient object with connection parameters set 
*/ 
private static HttpClient getHttpClient() { 
    if (mHttpClient == null) { 
     mHttpClient = new DefaultHttpClient(); 
     final HttpParams params = mHttpClient.getParams(); 
     HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT); 
     HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT); 
     ConnManagerParams.setTimeout(params, HTTP_TIMEOUT); 
    } 
    return mHttpClient; 
} 

/** 
* Performs an HTTP Post request to the specified url with the 
* specified parameters. 
* 
* @param url The web address to post the request to 
* @param postParameters The parameters to send via the request 
* @return The result of the request 
* @throws Exception 
*/ 
public static String executeHttpPost(String url, JSONObject json) throws Exception { 
    BufferedReader in = null; 
    try { 
     HttpClient client = getHttpClient(); 
     HttpPost request = new HttpPost(url); 
     request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF-8"))); 
     request.setHeader("Content-Type", "application/json"); 

     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 

     String result = sb.toString(); 
     return result; 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

/** 
* Performs an HTTP GET request to the specified url. 
* 
* @param url The web address to post the request to 
* @return The result of the request 
* @throws Exception 
*/ 
public static String executeHttpGet(String url) throws Exception { 
    BufferedReader in = null; 
    String data = null; 

    try { 
     HttpClient client = getHttpClient(); 
     HttpGet request = new HttpGet(); 
     request.setURI(new URI(url)); 
     HttpResponse response = client.execute(request); 
     response.getStatusLine().getStatusCode(); 

     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String l = ""; 
     String nl = System.getProperty("line.separator"); 
     while ((l = in.readLine()) !=null){ 
      sb.append(l + nl); 
     } 
     in.close(); 
     data = sb.toString(); 
     return data;   
    } finally{ 
     if (in != null){ 
      try{ 
       in.close(); 
       return data; 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

/** 
* Performs an HTTP DELETE request to the specified url. 
* 
* @param url The web address to post the request to 
* @return The result of the request 
* @throws Exception 
*/ 
public static String executeHttpDelete(String url) throws Exception { 
    BufferedReader in = null; 
    String data = null; 

    try { 
     HttpClient client = getHttpClient(); 
     HttpDelete request = new HttpDelete(); 
     request.setURI(new URI(url)); 
     HttpResponse response = client.execute(request); 
     response.getStatusLine().getStatusCode(); 

     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String l = ""; 
     String nl = System.getProperty("line.separator"); 
     while ((l = in.readLine()) !=null){ 
      sb.append(l + nl); 
     } 
     in.close(); 
     data = sb.toString(); 
     return data;   
    } finally{ 
     if (in != null){ 
      try{ 
       in.close(); 
       return data; 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

/** 
* Performs an HTTP Put request to the specified url with the 
* specified parameters. 
* 
* @param url The web address to post the request to 
* @param putParameters The parameters to send via the request 
* @return The result of the request 
* @throws Exception 
*/ 
public static String executeHttpPut(String url, JSONObject json) throws Exception { 
    BufferedReader in = null; 
    try { 
     HttpClient client = getHttpClient(); 
     HttpPut request = new HttpPut(url); 

     request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF-8"))); 
     request.setHeader("Content-Type", "application/json"); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 

     String result = sb.toString(); 
     return result; 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

}