Java – How do I get data from Azure database tables to an Android application?

How do I get data from Azure database tables to an Android application?… here is a solution to the problem.

How do I get data from Azure database tables to an Android application?

I’m new to Azure and 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 it into the GridView in android. I know that with the “padding” part of the adapter, I just want to know how to connect and receive data from the table 🙂

I tried this Microsoft’s tutorialsHowever, I had some difficulty 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 you specify a SQL SELECT query in this case? Looks like this code is not the way to do this? I want to populate a GridView 🙂 with tabular data

Solution

The most reliable way to get data from Azure Table Storage to Android is through the REST API. It’s relatively simple and you have full control.

For example,

to get (query) data, you can examine the following example 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 information:

Note that Azure Tables does not support the SQL language. Azure Tables is a completely different type of storage (so-called no-SQL).

Related Problems and Solutions