Backendless Android:如何从表中检索特定用户数据?

时间:2017-05-03 20:05:07

标签: android backendless

Android应用用户数据已保存到Backendless表中。我想从表中检索用户数据。

我尝试使用' where子句'但我不知道如何显示找到的数据。 https://backendless.com/docs/android/doc.html#data_search_with_where_clause

以下代码具有Backendless服务器错误1023。

void onFindContact() {

    IDataStore<Map> contactStorage = Backendless.Data.of( "Contact" );
    DataQueryBuilder queryBuilder = DataQueryBuilder.create();

    // set where clause
    queryBuilder.setWhereClause( "age is not null" );

    // set related columns
    queryBuilder.setRelated( "address", "phoneNumber" );

    // request sorting
    //queryBuilder.setSortBy( "name" );

    //set offset and page size
    //queryBuilder.setPageSize( 20 );
    //queryBuilder.setOffset( 40 );

    contactStorage.find( queryBuilder, new AsyncCallback<List<Map>>() {
        @Override
        public void handleResponse( List<Map> contactObjects ) {
            Log.i("MYAPP", "Retrieved " + contactObjects.size() + " objects");
            Toast.makeText(FindContactActivity.this, "Retrieved " + contactObjects.size() + " objects from Backendless table", Toast.LENGTH_LONG).show();

            BackendlessUser user = Backendless.UserService.CurrentUser();
            if (user != null) {
                // get user's phone number (i.e. custom property)
                String phoneNumber = (String) user.getProperty("phoneNumber");
                Toast.makeText(FindContactActivity.this, String.format("phone number: %s", phoneNumber), Toast.LENGTH_LONG).show();

                // get user's age (i.e. custom property)
                String age = (String) user.getProperty("age");
                Toast.makeText(FindContactActivity.this, String.format("age: %s", age), Toast.LENGTH_LONG).show();

                // get user's address (i.e. custom property)
                String adress = (String) user.getProperty("address");
                Toast.makeText(FindContactActivity.this, String.format("address: %s", adress), Toast.LENGTH_LONG).show();
            }
        }
        @Override
        public void handleFault( BackendlessFault fault )
        {
            Log.e( "MYAPP", "Server reported an error " + fault.getCode() );//compile error at 'getMessage'
            Toast.makeText(FindContactActivity.this,"Backendless Server reported an error "+ fault.getCode(), Toast.LENGTH_LONG).show();
        }
    });
    //</Map>  //compile error at '</map>'

    /**** from https://backendless.com/docs/android/doc.html#data_search_with_where_clause
     * Find all contacts where the value of the "age" property equals 47:
     *
    String whereClause = "age = 47";
    DataQueryBuilder queryBuilder = DataQueryBuilder.create();
    queryBuilder.setWhereClause( whereClause );


    Backendless.Data.of( "Contact" ).find( queryBuilder, new AsyncCallback<List<Map>>(){
                @Override
                public void handleResponse( List<Map> foundContacts )
                {
                    // every loaded object from the "Contact" table is now an individual java.util.Map
                }
                @Override
                public void handleFault( BackendlessFault fault )
                {
                    // an error has occurred, the error code can be retrieved with fault.getCode()
                }
    });
    */
}

}

1 个答案:

答案 0 :(得分:2)

Backendless文档中提供了一个数据检索示例:https://backendless.com/docs/android/doc.html#data_search_with_where_clause