【发布时间】:2018-09-20 00:17:22
【问题描述】:
我想要 wit.ai 分析我的句子。当我发送字符串“门在哪里”时,wit.ai 应该用 JSON 来回答,其中包括我的句子的意图:导航。但不幸的是 wit.ai 日志说没有请求进入。我做错了什么?参数是对的,但可能是顺序错误?
public class MainActivity extends AppCompatActivity {
String addressM = "https://api.wit.ai/message";
String accessToken = "xxxxxxxxxxxxxxxxxxxxccc";
String header = "Authorization: Bearer ";
String query = "q";
String message = "where is the door";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JSONTask().execute(addressM);
}
});
}
private class JSONTask extends AsyncTask<String, String, String >{
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("access_token", "xxxxxxxxxxxxxxxxxxxxxxx");
connection.setRequestProperty("q", message);
connection.setRequestMethod("POST");
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) !=null){
buffer.append(line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null){
connection.disconnect();
}
try {
if (reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(result);
Toast.makeText(MainActivity.this, result,
Toast.LENGTH_LONG).show();
}
}
}
这是一个响应应该是什么的示例。与我相关的是意图是导航
[
{
"entities":
{
"intent":
[
{
"confidence":
0.92597581019421
"value":
{
"value":
"navigation"
}
"entity":
"intent"
}
]
}
"confidence":
null
"_text":
"where is the door"
"intent":
"default_intent"
"intent_id":
"default_intent_id"
}
]
【问题讨论】:
-
承载令牌或 access_token 作为 req 参数?因为你两者都有。 wit.ai 的 HTTP 响应代码和正文是什么?
-
您可能需要编辑此问题以删除敏感信息(访问密钥)
-
@evilSnobu 我都试过了,但都没有奏效,我不知道我应该吃什么。
-
我添加了响应示例
标签: java android http httpurlconnection wit.ai