2016-12-03 10 views
0

私はapi.aiを呼び出すアンドロイドアプリを構築しています。私は応答を解析し、それをユーザーに表示したいと思います。以前の私は以下の通りであるNode.jsの中にコードを書いていた:Androidからapi.aiを呼び出す

function sendMessageToApiAi(options,botcontext) { 
     var message = options.message; // Mandatory 
     var sessionId = options.sessionId || ""; // optinal 
     var callback = options.callback; 
     if (!(callback && typeof callback == 'function')) { 
      return botcontext.sendResponse("ERROR : type of options.callback should be function and its Mandatory"); 
     } 
     var nlpToken = options.nlpToken; 

     if (!nlpToken) { 
      if (!botcontext.simpledb.botleveldata.config || !botcontext.simpledb.botleveldata.config.nlpToken) { 
       return botcontext.sendResponse("ERROR : token not set. Please set Api.ai Token to options.nlpToken or context.simpledb.botleveldata.config.nlpToken"); 
      } else { 
       nlpToken = botcontext.simpledb.botleveldata.config.nlpToken; 
      } 
     } 
     var query = '?v=20150910&query='+ encodeURIComponent(message) +'&sessionId='+context.simpledb.roomleveldata.c1+'&timezone=Asia/Calcutta&lang=en ' 
     var apiurl = "https://api.api.ai/api/query"+query; 
     var headers = { "Authorization": "Bearer " + nlpToken}; 
     botcontext.simplehttp.makeGet(apiurl, headers, function(context, event) { 
      if (event.getresp) { 
       callback(event.getresp); 
      } else { 
       callback({}) 
      } 
     }); 
    } 

私のAndroidのコードは以下の通りです:

package com.example.pramod.apidev; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.net.HttpURLConnection; 
import java.net.URL; 

public class MainActivity extends AppCompatActivity{ 
    private Button listenButton; 
    private TextView resultTextView; 
    private EditText inputText; 
    private static String API_URL = "https://api.api.ai/api/query"; 
    private static String API_KEY = "d05b02dfe52f4b5f969ba1257cffac37"; 
    private static String query; 
    private static String s; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listenButton = (Button) findViewById(R.id.listenButton); 

     resultTextView = (TextView) findViewById(R.id.resultTextView); 
     inputText = (EditText)findViewById(R.id.inputText); 

     s = inputText.getText().toString(); 


     query = "?v=20150910&query=hi" +"&sessionId=1480181847573api&timezone=Asia/Calcutta&lang=en"; 
     listenButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       try { 


        URL url = new URL(API_URL + query + "&apiKey=" + API_KEY); 
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
       } 
       catch(Exception e) { 
        Log.e("ERROR", e.getMessage(), e); 

       } 


      } 
     }); 

     } 
    } 

私は、次のヘルプが必要です

(I)どのようにエラー401が発生しているので、Api.aiに電話してください。 Node.jsコードを使用してApi.aiを正確に呼び出す方法を伝える人もいますか?

(ii)レスポンスを解析してユーザーに表示するにはどうすればよいですか?

ありがとうございます。

答えて

3

私はアンドロイドアプリからapi.aiに接続しています。

1.以下の通り行うこと

手順は、依存関係を追加します。

compile 'ai.api:libai:1.2.2' 
    compile 'ai.api:sdk:[email protected]' 

2. AIListenerを実装するアクティビティを作成します。

3.宣言AIService、およびAIDataService:

private AIService aiService; 
    private AIDataService aiDataService; 

4.初期設定ファイル、サービスおよびリスナーを追加します。

final ai.api.android.AIConfiguration config = new  ai.api.android.AIConfiguration("API_KEY", 
     ai.api.android.AIConfiguration.SupportedLanguages.Spanish, 
     ai.api.android.AIConfiguration.RecognitionEngine.System); 

    // Use with text search 
    aiDataService = new AIDataService(this, config); 

    // Use with Voice input 
    aiService = AIService.getService(this, config); 

    aiService.setListener(this); 

5.要求を行う非同期タスクを実行します:

AIRequest aiRequest = new AIRequest(); 

    aiRequest.setQuery(request); 

//リクエスト - チャットボットに送信して、対応する応答を得るための任意の文字列。

if(aiRequest==null) { 
     throw new IllegalArgumentException("aiRequest must be not null"); 
    } 

    final AsyncTask<AIRequest, Integer, AIResponse> task = 
     new AsyncTask<AIRequest, Integer, AIResponse>() { 
        private AIError aiError; 

    @Override 
    protected AIResponse doInBackground(final AIRequest... params) { 
     final AIRequest request = params[0]; 
      try { 
       final AIResponse response = aiDataService.request(request); 
       // Return response 
       return response; 
      } catch (final AIServiceException e) { 
       aiError = new AIError(e); 
       return null; 
      } 
     } 

    @Override 
    protected void onPostExecute(final AIResponse response) { 
     if (response != null) { 
      onResult(response); 
     } else { 
      onError(aiError); 
     } 
    } 
}; 
task.execute(aiRequest); 

6はonResult方法

Result result = response.getResult(); 

結果が文字列である場合、応答の文字列は、であろう。

String speech = result.getFulfillment().getSpeech(); 

よろしく。

https://github.com/api-ai/apiai-android-client

ハッピーコーディング - ヌリアは

+0

'aiRequest.setQuery(request);の'要求 'とは何ですか?あなたは答えにどこで定義しましたか? –

0

あなたはここにAPI.AIを統合する公式ドキュメントを参照してくださいすることができます。

+1

答えがリンクなしで問題を解決していることを確認してください。リンクが切れた場合に備えて、短い要約を追加する必要があります。 –

関連する問題