Skip to content Skip to sidebar Skip to footer

Custom API In Azure APP Serivce Examples Searched For Android Client

I need a working example for a custom API for Microsoft Azure App Service. I could not get any useful or working information/examples for that, or they just show each time differe

Solution 1:

// EDIT - Added / edited client / server code to Post a String.

You can use the following code to do a GET request on the auto generated API controller Visual Studio creates (ValuesController).

private void getStringFromAzure() throws MalformedURLException {

    // Create the MobileService Client object and set your backend URL
    String yourURL = "https://yourApp.azurewebsites.net/";
    MobileServiceClient mClient = new MobileServiceClient(yourURL, this);

    // Your query pointing to yourURL/api/values
    ListenableFuture<JsonElement> query = mClient.invokeApi("values", null, GetMethod, null);

    // Callback method
    Futures.addCallback(query, new FutureCallback<JsonElement>() {
        @Override
        public void onSuccess(JsonElement jsonElement) {

            // You are expecting a String you can just output the result.
            final String result = jsonElement.toString();

            // Since you are on a async task, you need to show the result on the UI thread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
        public void onFailure(Throwable throwable) {
            Log.d(TAG, "onFailure: " + throwable.getMessage());
        }
    });
}

public void sendString(final String someString) throws MalformedURLException {

        // Your query pointing to /api/values/{String}
        ListenableFuture<JsonElement> query = mClient.invokeApi("values/" + someString, null, PostMethod, null);

        // Callback method
        Futures.addCallback(query, new FutureCallback<JsonElement>() {
            @Override
            public void onSuccess(JsonElement jsonElement) {

                // You are expecting a String you can just output the result.
                final String result = jsonElement.toString();
            }

            @Override
            public void onFailure(Throwable throwable) { }
        });
}

The backend API: (ValuesController)

{
    // Use the MobileAppController attribute for each ApiController you want to use  
    // from your mobile clients 
    [MobileAppController]
    public class ValuesController : ApiController
    {
        // GET api/values
        public string Get()
        {
            return "Hello World!";
        }

        // POST api/values/inputString
        public string Post(string inputString)
        {
            return inputString;
        }
    }
}

You can also send parameters along in the following way:

List<Pair<String, String>> parameters = new ArrayList<>();

parameters.add(new Pair<>("name", "John"));
parameters.add(new Pair<>("password", "fourwordsalluppercase"));

ListenableFuture<JsonElement> query = client.invokeApi("yourAPI", PostMethod, parameters);

Or as json in the body:

JsonObject body = new JsonObject();

body.addProperty("currentPassword", currentPassword);
body.addProperty("password", password);
body.addProperty("confirmPassword", confirmPassword);

ListenableFuture<JsonElement> query = mClient.invokeApi("yourAPI", body, PostMethod, null);

Solution 2:

Based on my understanding, I think there are two parts in your question which include as below. And I think you can separately refer to two sections to get the answers and write your own example.

  1. How to define a custom API on Azure Mobile App to retrieve data from database? Please refer to the section Custom APIs to know how to do with Azure Mobile App backend.

  2. How to call a custom API from Android App? Please refer to the section How to: Call a custom API to know how to do with Android SDK.


Post a Comment for "Custom API In Azure APP Serivce Examples Searched For Android Client"