【发布时间】:2020-11-15 05:48:22
【问题描述】:
我正在使用 rasa 创建聊天机器人,我想将该 rasa 聊天机器人集成到我的 Android 应用程序中。
public void Test(String s) throws JSONException {
String url = "http://192.168.0.105:5005/webhooks/rest/webhook";
final String[] myObjAsString = {""};
JSONObject jsonBody = null;
try {
jsonBody = new JSONObject("{\"message\":\""+s+"\",\"sender\":\"Me\"}");
} catch (JSONException e) {
e.printStackTrace();
}
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, url, jsonBody,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR", error.toString());
}
}
);
requestQueue.add(jsonObjectRequest);
}
以上是我向机器人发送请求的代码。对于测试,我使用的是 rasa 提供的默认训练机器人。 但我收到以下错误。
2020-07-25 19:07:52.418 6615-6615/com.example.bot D/ERROR: com.android.volley.ParseError: org.json.JSONException: Value [{"recipient_id":"Me","text":"Hey! How are you?"}] of type org.json.JSONArray cannot be converted to JSONObject
bot 接受响应为 jsonobject 并响应 jsonarray? 如何解决这个问题?
【问题讨论】: