gpt4 book ai didi

android - 使用 REST 接口(interface) : Cannot connect 连接到数据库

转载 作者:搜寻专家 更新时间:2023-11-01 09:10:06 24 4
gpt4 key购买 nike

我正在尝试通过正在开发的 Android 应用程序中的 mongolabs REST 接口(interface)连接到 mongodb,但它没有连接,而是抛出异常(或者至少我认为是)。我对后端不熟悉,所以如果我犯了一个致命的菜鸟错误,请原谅我。这是日志

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)

下面是我编写的用于访问数据库并获取名称等项目的 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);

最佳答案

您实际上需要显式设置 HttpClient 来处理 SSL。我相信这个 stackoverflow 线程有您需要的信息:

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);
}

关于android - 使用 REST 接口(interface) : Cannot connect 连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8811840/

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