delight-im / Android-DDP

[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Apache License 2.0
274 stars 54 forks source link

How to query database? #11

Closed iahvector closed 9 years ago

iahvector commented 9 years ago

how can I call collection.find() on android? meteor.call("/Collection/find") gives 404 despite that it works on the web client.

ocram commented 9 years ago

Can you make sure the collection does actually exist and that your syntax is correct?

The call(...) method is used just like this in the source code of this library itself: https://github.com/delight-im/Android-DDP/blob/017a82ad1cc40ff4c4e8ecc3ba6d59a2d68bf317/Android/Source/src/im/delight/android/ddp/Meteor.java#L629

Moreover, the examples have a call to MongoDB's db.collection.count() implemented with this library's call(...) method: https://github.com/delight-im/Android-DDP/blob/017a82ad1cc40ff4c4e8ecc3ba6d59a2d68bf317/Examples/DDP/src/im/delight/android/ddp/examples/MainActivity.java#L101

iahvector commented 9 years ago

This is my code:

if (meteor.isConnected()) {
    meteor.loginWithEmail(email, password, new ResultListener() {
        @Override
        public void onSuccess(String result) {
            Log.d(TAG, "Logged in: " + result);

            try {
                JSONObject login = new JSONObject(result);

                String userId = login.getString("id");
                String token = login.getString("token");
                long expiry = login.getJSONObject("tokenExpires").getLong("$date");

                Map<String, Object> user = new HashMap<String, Object>();
                user.put("_id", userId);

                Object[] queryParams = {user};

                meteor.call("/Users/find", queryParams, new ResultListener() {
                    @Override
                    public void onSuccess(String result) {
                        Log.d(TAG, "Call result: " + result);
                    }

                    @Override
                    public void onError(String error, String reason, String details) {
                        Log.d(TAG, "Error: " + error + " " + reason + " " + details);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(String error, String reason, String details) {
            Log.d(TAG, "Error: " + error + " " + reason + " " + details);
        }
    });
}
ocram commented 9 years ago

Thanks for sharing the code!

There's nothing wrong with this library. It's just that Meteor doesn't offer find to be called directly. The only methods you can call on a collection are insert, update and remove. And for those, there are dedicated methods in this library so you don't need to use call(...).

Conceptually, retrieving data via find is not how Meteor works. In Meteor, you subscribe to data and the server pushes all changes to you via the callbacks of this library.

So instead of writing meteor.call("/Users/find", ...), you have to publish the data on the server and subscribe on the client with this library.

isdzulqor commented 8 years ago

how do you handle meteor.call("/Users/find", ...) in server side? when we use meteor.call so we must make meteor.method in server, right.. but I still don't understand in client it needs return data and the name of the meteor.call is "/user/find". Is the name in meteor.method must /user/find too? and how to handle return data ? Is the return data is json type? Could you give some example please,,, Thank you..

iahvector commented 8 years ago

@isdzulqor as @mwaclawek said above, you can't use /Users/find. What I do is subscribe to the data I want and save them to a local database, practically replicating the functionality of MiniMongo

meteor.setCallback(new MeteorCallback() {

    // ...

    @Override
    public void onDataAdded(String collectionName, String documentID, String fieldsJson) {
        // if documentID exists in the local db, update with the new data
        // else insert the new data
    }

    // ...
});
isdzulqor commented 8 years ago

@iahvector so it needs SQLite in your android native, and it needs to parse the JSON? Is it correct?

iahvector commented 8 years ago

Yes, though not necessarily SQLite, you can use any DB available on android. I personally use SQLite with an ORM. Sugar ORM is the easiest, and I'm currently experimenting with FlowDB as it it provides broader functionality.

isdzulqor commented 8 years ago

@iahvector Thank you so much for sharing this, My english is still bad, so sometimes I feel hard to understanding the documentation of the library,, I will try that,, Is it possible to upload file wtih this ddp library?

iahvector commented 8 years ago

It can be, the guys at collectionFS do it, but I think it is complicated. When I needed to upload a file from android, I created a HTTP POST API for uploading files.

isdzulqor commented 8 years ago

HTTP POST API with restivus? you use Meteor for server or php when it needs to upload file?

iahvector commented 8 years ago

I use meteor.

isdzulqor commented 8 years ago

@iahvector would you like to show me the code to make HTTP POST API to upload File like image? because I've implemented the tutorial from google but it's not working,

ocram commented 8 years ago

Thanks for the interesting discussion, @isdzulqor and @iahvector :)

But please try to open new issues for new problems and questions again :) The reason is that your questions and answer may really be helpful for other users and they may more easily find them if they are in separate issues.

Thank you!