2016-08-09 10 views
0

Microsoft Azure App ServiceのカスタムAPIの実例が必要です。 私はそれに関する有益な情報や実例を得ることができませんでした。あるいは、時代遅れの異なるアプローチを毎回表示していましたか? 今のところ私はデータベースから情報を取得し、それを私のAndroidクライアントに返すワーキングテーブルコントローラを持っています。今私はカスタムAPIコントローラを定義して、文字列を戻す必要があります。これらの例では、オブジェクトを返すために、すべてサービスにオブジェクトを送信しています。私はAPIに何かを送るつもりはなく、ちょうどGETリクエストからいくつかの情報を取り戻すだけです。私の理解に基づいてAzureのカスタムAPI APPクライアントの検索例

よろしく

答えて

1

// EDIT - 文字列を投稿するためのクライアント/サーバーコードの追加/編集。

Visual Studioによって作成される自動生成APIコントローラ(ValuesController)に対して、次のコードを使用してGETリクエストを行うことができます。

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)

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

また、次のように沿ってパラメータを送信することができます

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のように:

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

解決策をもう一度ありがとう。私は文字列を渡すことができ、バックエンドAPIはそれを受け取ることができる "Post"メソッドの実例を私に提供する方法はありますか?前もって感謝します! –

+0

問題ありません! :] Stringを送信するためのコードと、バックエンドにデータを送信する2つの方法を追加しました。私はコードをテストしませんでした.. 幸運! –

+0

ありがとうございます!私はそれを解決していましたが、2日で解決しました。D AzureモバイルサービスのカスタムAPIに関するサーバー側の問題と、その文字列を受け取る方法と解析する方法が多かったです。 –

0

を、私は以下のように含まれ、あなたの質問に2つの部分があると思います。そして、あなたは別々に2つのセクションを参照して回答を得て、独自の例を書くことができると思います。

  1. Azure Mobile AppでカスタムAPIを定義してデータベースからデータを取得するにはどうすればよいですか? Azure Mobile Appバックエンドを使用する方法については、Custom APIsのセクションを参照してください。

  2. Android AppからカスタムAPIを呼び出す方法はありますか? Android SDKの操作方法については、How to: Call a custom APIのセクションを参照してください。

+0

こんにちは!あなたの情報をありがとう。私はすでにその文書を知っており、それは問題です。実際の例はなく、2行のコードしかありません。 (参照:https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/;カスタムAPIコントローラを定義する方法;ステップ4)。 GETリクエストの実際の例と、オブジェクトにリクエストを渡すことなくAndroid用の特定のGETリクエストのAPI呼び出しが必要です。 Androidアプリに文字列を戻したい –

関連する問題