简体   繁体   English

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

[英]How to get data from MongoDB with Android AsyncHttpClient

Background: I'm working on an Android project that was handed off to me and there are some things I'm still trying to learn. 背景:我正在处理一个交给我的Android项目,有些事情我还在努力学习。 The people on the project before me left no documentation, so forgive me. 在我之前参与项目的人员没有留下任何文档,请原谅我。

Question: Basically I want to know how to get data back from our MongoDB (I think). 问题:基本上,我想知道如何从我们的MongoDB中取回数据(我认为)。 Using AsyncHttpClient params are put sent via post 使用AsyncHttpClient参数是通过post发送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.");
  }
});

So the above code is basically updating a profile and sending the params via http post. 因此,以上代码基本上是更新配置文件并通过http post发送参数。

Later there is a get on the client: 后来有一个客户端:

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

What I don't understand is where "session/updateProfile" and "session/getUserId" come from. 我不明白的是"session/updateProfile""session/getUserId"来自"session/getUserId" Is this an API call, if so how can I find what is available and also how to add to it? 这是一个API调用吗,如果可以的话,如何找到可用的以及如何添加呢?

Update 更新资料

Found this code in a JavaScript file for the website. 在网站的JavaScript文件中找到此代码。 Maybe this can lead someone to help me put everything together? 也许这可以导致某人帮助我将所有内容整合在一起?

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

Do I need to add to this JS file to expand the API? 我需要添加到此JS文件中以扩展API吗?

where [do] "session/updateProfile" and "session/getUserId" come from? “ session / updateProfile”和“ session / getUserId”来自哪里? Is this an API call 这是API调用吗

Yup, REST API call to wherever Utilities.getBaseURL() points to. 是的,无论Utilities.getBaseURL()指向何处,都需要进行REST API调用。

how can I find what is available? 我如何找到可用的?

Well, if that server is yours, then you'll need to go find some code / documentation for that. 好吧,如果该服务器是您的服务器,那么您需要查找一些代码/文档。 Otherwise, if it's some other external service, you'll still have to find some API documentation or contact someone who does know about it. 否则,如果它是其他一些外部服务,则您仍然必须找到一些API文档或联系对此有所了解的人。 You've mentioned in the comments that it is DigitalOcean, so it isn't an external service. 您在评论中提到它是DigitalOcean,因此它不是外部服务。 There is additional server side code to inspect. 还有其他服务器端代码需要检查。

how to add to it? 如何添加呢?

If it were an external service, you probably couldn't. 如果它是一项外部服务,您可能不会。 Since this DigitalOcean server could be running literally any service, it entirely depends on the server side programming language. 由于此DigitalOcean服务器实际上可以运行任何服务,因此它完全取决于服务器端编程语言。 You've mentioned that Javascript file along with MongoDB, so I'm guessing it's some NodeJS variant. 您已经提到Javascript文件和MongoDB,所以我猜它是一些NodeJS变体。 You mention MeteorJS in the comments, so that's the documentation you need to add new features. 您在注释中提到了MeteorJS,因此这是添加新功能所需的文档。

Do I need to add to this JS file to expand the API? 我需要添加到此JS文件中以扩展API吗?

Simply: yes. 简单:是的。

I want to know how to get data back from our MongoDB 我想知道如何从我们的MongoDB中取回数据

Essentially this line is doing that. 本质上,这条线就是这样做的。 Of course, the response code shouldn't always be 200 and the content of the response string can be anything you want, but probably should be JSON, or at least something Android can parse 当然,响应代码不应该总是200,响应字符串的内容可以是您想要的任何内容,但可能应该是JSON,或者至少是Android可以解析的内容

JsonRoutes.sendResult(res, 200, response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM