gpt4 book ai didi

java - 如何使用 Android AsyncHttpClient 从 MongoDB 获取数据

转载 作者:行者123 更新时间:2023-12-01 09:21:54 24 4
gpt4 key购买 nike

背景:我正在开发一个交给我的 Android 项目,有些东西我仍在努力学习。在我之前参与该项目的人没有留下任何文档,所以请原谅我。

问题:基本上我想知道如何从我们的 MongoDB 取回数据(我认为)。使用 AsyncHttpClient 参数通过 post

发送
private AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("authorization", getSharedPreferences("Prefs", Context.MODE_PRIVATE).getString("authToken", ""));
params.put("name", name);
params.put("email", email);
client.post(Utilities.getBaseURL() + "session/updateProfile", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
HashMap<String, String> profile = new HashMap<>();
profile.put("user_name", name);
profile.put("user_email", email);
Utilities.updateAllDetails(profile);
}

@Override
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, JSONObject response) {
Utilities.showUserDialog(context, "Profile Change Error", "Attention, there was an error in updating your profile, please try again.");
}
});

所以上面的代码基本上是更新配置文件并通过 http post 发送参数。

稍后客户端有一个get:

client.get(Utilities.getBaseURL() + "session/getUserId", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
try {
userId = response.getString("responseString");
startActivityForResult(intent, PICK_IMAGE);
} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, JSONObject response) {}
});

我不明白的是“session/updateProfile”“session/getUserId”来自哪里。这是一个 API 调用吗?如果是,我怎样才能找到可用的内容以及如何添加它?

更新

在网站的 JavaScript 文件中找到此代码。也许这可以引导有人帮助我把所有东西放在一起?

JsonRoutes.add('get', '/api/v1/session/getUserId', function(req, res, next)     {
var authToken = req.headers["authorization"];
var userObj = verifyUser(authToken);
var response = {};
if (userObj) {
response.responseString = userObj._id;
JsonRoutes.sendResult(res, 200, response);
} else {
response.responseString = "user not found";
JsonRoutes.sendResult(res, 200, response);
}
});

我需要添加这个JS文件来扩展API吗?

最佳答案

where [do] "session/updateProfile" and "session/getUserId" come from? Is this an API call

是的,REST API 调用到 Utilities.getBaseURL() 指向的任何地方。

how can I find what is available?

好吧,如果该服务器是您的,那么您需要找到一些代码/文档。否则,如果是其他外部服务,您仍然需要查找一些 API 文档或联系了解它的人。您在评论中提到它是 DigitalOcean,因此它不是外部服务。还有额外的服务器端代码需要检查。

how to add to it?

如果它是外部服务,您可能无法做到。由于该 DigitalOcean 服务器实际上可以运行任何服务,因此它完全取决于服务器端编程语言。您已经提到了 Javascript 文件和 MongoDB,所以我猜测它是 NodeJS 的某种变体。您在评论中提到了 MeteorJS,因此这是您添加新功能所需的文档。

Do I need to add to this JS file to expand the API?

简单地说:是的。

I want to know how to get data back from our MongoDB

本质上这条线就是这样做的。当然,响应代码不应该总是 200,响应字符串的内容可以是任何你想要的,但可能应该是 JSON,或者至少是 Android 可以解析的东西

JsonRoutes.sendResult(res, 200, response);

关于java - 如何使用 Android AsyncHttpClient 从 MongoDB 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121584/

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