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).

privatevoidgetStringFromAzure()throws MalformedURLException {

    // Create the MobileService Client object and set your backend URLStringyourURL="https://yourApp.azurewebsites.net/";
    MobileServiceClientmClient=newMobileServiceClient(yourURL, this);

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

    // Callback method
    Futures.addCallback(query, newFutureCallback<JsonElement>() {
        @OverridepublicvoidonSuccess(JsonElement jsonElement) {

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

            // Since you are on a async task, you need to show the result on the UI thread
            runOnUiThread(newRunnable() {
                @Overridepublicvoidrun() {
                    Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
                }
            });
        }

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

publicvoidsendString(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, newFutureCallback<JsonElement>() {
            @OverridepublicvoidonSuccess(JsonElement jsonElement) {

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

            @OverridepublicvoidonFailure(Throwable throwable) { }
        });
}

The backend API: (ValuesController)

{
    // Use the MobileAppController attribute for each ApiController you want to use  // from your mobile clients 
    [MobileAppController]
    publicclassValuesController : ApiController
    {
        // GET api/valuespublicstringGet()
        {
            return"Hello World!";
        }

        // POST api/values/inputStringpublicstringPost(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:

JsonObjectbody=newJsonObject();

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"