Azure APP Serivce示例中自定义API搜索Android客户端 [英] Custom API in Azure APP Serivce examples searched for Android Client

查看:11
本文介绍了Azure APP Serivce示例中自定义API搜索Android客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个适用于 Microsoft Azure 应用服务的自定义 API 的工作示例.我无法获得任何有用或有效的信息/示例,或者它们只是每次都显示过时的不同方法?!?!现在我有一个工作表控制器,它从数据库中获取信息并将其返回给我的 Android 客户端.现在我需要定义一个自定义 API 控制器来获取字符串.在示例中,它们都向服务发送对象以取回对象.我不想向 API 发送任何内容,只是从 GET 请求中检索一些信息.

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 different approaches which are outdated?!?! For now I have a working table controller which gets information from database and returns it back to my Android client. Now I need to define a custom API Controller to get a string back. In the examples they are all sending an object to the service in order to get an object back. I do not want to send anything to the API, just retrieve some information back from a GET Request.

问候

推荐答案

//EDIT - 添加/编辑客户端/服务器代码以发布字符串.

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

您可以使用以下代码对 Visual Studio 创建的自动生成的 API 控制器 (ValuesController) 执行 GET 请求.

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) { }
        });
}

后端 API:(ValuesController)

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

或者作为正文中的json:

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

这篇关于Azure APP Serivce示例中自定义API搜索Android客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆