How to Get data from Azure DB Table to an Android

2019-02-15 05:00发布

I'm new to azure, I don't know how to connect to a Table I created on an Azure Database. I want to get the table data (SELECT *) and populate them in a GridView in android. I know the 'populating' part using an adapter, all I want to know is how to connect and receive the data from the Table :)

I've tried THIS tutorial by Microsoft however, I'm facing some difficulties when applying the same tutorial to my scenario.

Here's what I tried:

@Override

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



setContentView(R.layout.activity_home_screen);

    try {
        mClient = new MobileServiceClient(
                "LINK",
                "KEY",
                this);

} catch (MalformedURLException e) {

}

    refreshItemsFromTable();
 }



private void refreshItemsFromTable()
{
          //ActivityData is my Entity class 

        new AsyncTask<Void, Void, Void>() {
            //
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    //This is the problematic area. My actual table's name is "Activity" on the Azure SQL
                    final MobileServiceList<ActivityData> result = mToDoTable.eq(false).execute().get();
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            mAdapter.clear();

                            for (ActivityData item : result) {
                                mAdapter.add(item);
                            }
                        }
                    });
                } catch (Exception exception) {
                    Toast.makeText(HomeScreen.this, exception.toString(),Toast.LENGTH_LONG);
                }
                return null;
            }
        }.execute();
    }

How do I specify the SQL SELECT query in this scenario? Looks like this code isn't the way to do it? I would like to populate a GridView with the table data :)

1条回答
淡お忘
2楼-- · 2019-02-15 06:06

Most sure way that you will get data from Azure Table Storage to your android is via REST API. It is relatively straight forward and you have full control.

For example, to fetch (query) data you can check the following sample request:

Request Syntax:
GET /myaccount/Customers()?$filter=(Rating%20ge%203)%20and%20(Rating%20le%206)&$select=PartitionKey,RowKey,Address,CustomerSince  HTTP/1.1

Request Headers:
x-ms-version: 2013-08-15
x-ms-date: Mon, 25 Nov 2013 15:25:14 GMT
Authorization: SharedKeyLite myaccount:<some key>
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
DataServiceVersion: 2.0;NetFx
MaxDataServiceVersion: 2.0;NetFx

More on that:

Please note that Azure tables does not support SQL language. Azure Tables are totally different type of storage (so called no-sql).

查看更多
登录 后发表回答