Java - How to Get and Use One Value from Azure Mobile Services on Android
I'm new to azure, but I know some things like how to fetch and store data for azure, I followed the official documentation for this purpose.
Link here - https://azure.microsoft.com/en-in/documentation/articles/mobile-services-android-get-started-data/
But the problem is this tutorial only shows you how to retrieve and use data from azure tree using adapters and lists. I want to know how can I get one value from azure mobile services and how to use it in android .
Plzz will provide me with both the internal code (if any) and the Java code to do this. Thanks in advance
source to share
I decided. There is no need to create a custom API.
Just follow the basics, Here is the code: -
final String[] design = new String[1];
private MobileServiceTable<User> mUser;
mUser = mClient.getTable(User.class);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
final MobileServiceList<User> result =
mUser.where().field("name").eq(x).execute().get();
for (User item : result) {
// Log.i(TAG, "Read object with ID " + item.id);
desig[0] = item.getDesignation(); //getDesignation() is a function in User class ie- they are getters and setters
Log.v("FINALLY DESIGNATION IS", desig[0]);
}
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
designation.setText(desig[0]);
}
}.execute();
DON'T forget to create a class User
for serialization
and everyone. Also you must define an array.
SENSITIVELY FREE to write if it doesn't work for you.
EDIT: -
design[0]
- array with size 1.
eq(x)
equals x
where the variable x
contains the username that I want to assign the database to (azure).
source to share
You can do this with a custom API. See this link: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#custom-api
The code looks like this:
exports.post = function(request, response) {
response.send(200, "{ message: 'Hello, world!' }");
}
Then it will reach meaning https://todolist.azure-mobile.net/api/APIFILENAME
.
If you want to access the table, you can do something like:
exports.post = function(request, response) {
var userTable = tables.getTable('users');
permissionsTable
.where({ userId: user.userId})
.read({ success: sendUser });
}
function sendUser(results){
if(results.length <= 0) {
res.send(200, {});
} else {
res.send(200, {result: results[0]});
}
}
Then you can follow the instructions for using the API on your Android client here: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-call-custom-api/
The way your application is written will change the way this code works / looks, but it looks something like this:
ListenableFuture<MarkAllResult> result = mClient.invokeApi( "UsersAPI", MarkAllResult.class );
This calls the API. You need to write a class and future to handle the results. The page above explains this in detail.
source to share
The most optimal solution would be to create an api on your server that accepts an id to return one object / tablerow.
In your android app, you only need to call:
MobileServiceTable<YourClass> mYourTable;
mClient = new MobileServiceClient(
"https://yoursite.azurewebsites.net/",
mContext);
mYourTable = mClient.getTable(YourClass.class);
YourClass request = mYourTable.lookUp(someId).get();
// request -> https://yoursite.azurewebsites.net/tables/yourclass/someId
YourClass
must have the same properties as the object on the server.
source to share