在unity中使用UNTI 怎么解析百度AI返回
ZYuXinz 发布于2017-11 浏览:6982 回复:21
0
收藏

下面是我的代码 我想解析出ai返回的话在(unity中)

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using System.Text;
[Serializable]
public class MyJsonClass
{
public string access_token;
}

[Serializable]
public class MyAIJsonClass
{
public int scene_id;
public string query;
public string session_id;
}

public class BaiDuAI : MonoBehaviour
{
public Text p;
public InputField ip;
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
public static string TOKEN = "24.adda70c11b9786206253ddb70affdc46.2592000.1493524354.282335-1234567";
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static string clientId = "kxkKvExXYkczGy55KqrugfGj";
// 百度云中开通对应服务应用的 Secret Key
private static string clientSecret = "ueRkTIqCpOufpuIyo6QWrky8hyjSwH7r";
public void GetAccessToken()
{
StartCoroutine(getAccessToken());
}
public IEnumerator getAccessToken() {
string authHost = "https://aip.baidubce.com/oauth/2.0/token";
WWWForm f = new WWWForm ();
f.AddField ("grant_type", "client_credentials");
f.AddField ("client_id", clientId);
f.AddField ("client_secret", clientSecret);
WWW w = new WWW (authHost,f);
yield return w;
MyJsonClass v1 = JsonUtility.FromJson (w.text) as MyJsonClass;
TOKEN = v1.access_token;
Debug.Log (TOKEN);
p.text = TOKEN;
}
public void GetAIText()
{
StartCoroutine(getAIText());
}
public IEnumerator getAIText() {
string AiHost = "https://aip.baidubce.com/rpc/2.0/solution/v1/unit_utterance?access_token=" + TOKEN;
//WWWForm f = new WWWForm ();
//f.AddField ("scene_id",9596 );
//f.AddField ("query", ip.text);
//f.AddField ("session_id", "");
MyAIJsonClass vvp = new MyAIJsonClass ();
vvp.scene_id = 9596;
vvp.query = ip.text;
vvp.session_id = "";
byte[] postBytes =Encoding.UTF8.GetBytes(JsonUtility.ToJson(vvp));
Dictionary header = new Dictionary();
header["Content-Type"] = "application/json;charset=UTF-8";
WWW w = new WWW (AiHost,postBytes,header);
yield return w;
//if (w.error == null)
{
Debug.Log ( w.text);
p.text = w.text;
}
}

收藏
点赞
0
个赞
共21条回复 最后由goJhou回复于2018-03
#2用户已被禁言回复于2017-11

你自己的APIKEY SERCETKEY都暴露了。

0
TOP
切换版块