封装:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System;
using System.Text;
public class TuLingMgr : MonoBehaviour {
private string url = "http://openapi.tuling123.com/openapi/api/v2";
private string apiKey = "2801fb36fe654af8b600816dee258338";
private string userId = "198119";
/// <summary>
/// 获取答案
/// </summary>
/// <param name="message"></param>
public void GetAnswerByQuestion(string question,Action<string> finishHandle)
{
StartCoroutine(HttpPost(question,finishHandle));
}
IEnumerator HttpPost(string message, Action<string> finishHandle)
{
//JsonData可以表示JsonObject{},也可以表示JsonArry[]//4e03ee9c4e8cc2af//"1512267543"
JsonData request = new JsonData();
//perception
request["perception"] = new JsonData();
request["perception"]["inputText"] = new JsonData();
request["perception"]["inputText"]["text"] = message;
//userInfo
request["userInfo"] = new JsonData();
request["userInfo"]["apiKey"] = apiKey;
request["userInfo"]["userId"] = userId;
//JsonMapper.ToJson(request)
//将Json对象转为Json字符串,直接ToString容易出错
//将Json字符串转为字节数组
//进行一个网络推送
WWW post = new WWW(url, Encoding.UTF8.GetBytes(JsonMapper.ToJson(request)));
yield return post;
Debug.Log(post.text);
JsonData response = JsonMapper.ToObject(post.text);
string result = response["results"][0]["values"]["text"].ToString();
Debug.Log(result);
if (finishHandle!=null)
{
finishHandle(result);
}
}
private static volatile TuLingMgr instance;
private static GameObject _container;
private static object syncRoot = new object();
public static TuLingMgr Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
TuLingMgr[] instance1 = FindObjectsOfType<TuLingMgr>();
if (instance1 != null)
{
for (var i = 0; i < instance1.Length; i++)
{
Destroy(instance1[i].gameObject);
}
}
}
}
GameObject go = new GameObject(typeof(TuLingMgr).Name);
_container = go;
DontDestroyOnLoad(go);
instance = go.AddComponent<TuLingMgr>();
}
return instance;
}
}
public virtual void Awake()
{
TuLingMgr t = gameObject.GetComponent<TuLingMgr>();
if (t == null)
t = gameObject.AddComponent<TuLingMgr>();
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = t;
}
if (instance != t)
{
MonoBehaviour[] monos = gameObject.GetComponents<MonoBehaviour>();
if (monos.Length > 1)
{
Destroy(t);
}
else
{
Destroy(gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.Text;
using UnityEngine.UI;
public class TuLingTest : MonoBehaviour
{
private string url = "http://openapi.tuling123.com/openapi/api/v2";
private string apiKey = "你的apiKey";
private string userId = "你的用户名id";
public InputField SpeakInputText; //你说的话
public Text text; //一问一答 呈现
private void Update()
{
if (Input.GetKeyDown(KeyCode.A)) //按A发送消息,(鼠标要移出输入框)
{
HttpPostFunc(SpeakInputText.text);
}
}
public void HttpPostFunc(string message)
{
StartCoroutine(HttpPost(message));
}
IEnumerator HttpPost(string message)
{
//JsonData可以表示JsonObject{},也可以表示JsonArry[]//4e03ee9c4e8cc2af//"1512267543"
JsonData request = new JsonData();
//perception
request["perception"] = new JsonData();
request["perception"]["inputText"] = new JsonData();
request["perception"]["inputText"]["text"] = message;
//userInfo
request["userInfo"] = new JsonData();
request["userInfo"]["apiKey"] = apiKey;
request["userInfo"]["userId"] = userId;
//JsonMapper.ToJson(request)
//将Json对象转为Json字符串,直接ToString容易出错
//将Json字符串转为字节数组
//进行一个网络推送
WWW post = new WWW(url, Encoding.UTF8.GetBytes(JsonMapper.ToJson(request)));
yield return post;
Debug.Log(post.text);
JsonData response = JsonMapper.ToObject(post.text);
string result = response["results"][0]["values"]["text"].ToString();
Debug.Log(result);
text.text += "我:" + message + "\n" + "机器人:" + result + "\n";
}
}
|