Reputation: 315
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. 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). Using AsyncHttpClient
params are put sent via 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.
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. Is this an API call, if so how can I find what is available and also how to add to it?
Update
Found this code in a JavaScript file for the website. 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?
Upvotes: 0
Views: 655
Reputation: 191681
where [do] "session/updateProfile" and "session/getUserId" come from? Is this an API call
Yup, REST API call to wherever Utilities.getBaseURL()
points to.
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. You've mentioned in the comments that it is DigitalOcean, so it isn't an external service. 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. You've mentioned that Javascript file along with MongoDB, so I'm guessing it's some NodeJS variant. You mention MeteorJS in the comments, so that's the documentation you need to add new features.
Do I need to add to this JS file to expand the API?
Simply: yes.
I want to know how to get data back from our 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
JsonRoutes.sendResult(res, 200, response);
Upvotes: 1