Android使用图灵机器人实现防QQ聊天(volley框架)

Android使用图灵机器人实现防QQ聊天(volley框架)_第1张图片

去图灵http://www.tuling123.com/member/robot/1268323/center/frame.jhtml?page=0&child=0

申请

apiKey   userId


代码Acitivty

  String josn = "{\"reqType\":0,\"perception\": {\"inputText\": {\"text\": \"" + tit + "\"}},\"userInfo\": {\"apiKey\": \"你的apiKey\",\"userId\": \"你的userId\"}}";
    try {
        jsonObject = new JSONObject(josn);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://openapi.tuling123.com/openapi/api/v2", jsonObject, new Response.Listener() {
        @Override
        public void onResponse(JSONObject response) {
            // TODO Auto-generated method stu
            Log.i("gdfgdfg", response.toString());
            String str = response.toString();
            try {
                JSONObject jsonObject = new JSONObject(str);
                JSONArray jsonArray=jsonObject.getJSONArray("results");
                Log.i("1", String.valueOf(jsonArray));
                JSONObject jsonObject1= (JSONObject) jsonArray.get(0);
                Log.i("2", String.valueOf(jsonObject1));
                JSONObject jsonObject2=jsonObject1.getJSONObject("values");
                Log.i("3", String.valueOf(jsonObject2));
                String text=jsonObject2.getString("text");
                Log.i("4", text);
                Map map = new HashMap<>();
                map.put("img", R.drawable.kefu);
                map.put("text", text);
                list.add(map);
                myBase.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
            Log.i("volley", error.toString());

        }
    });
    mQueue.add(jsonObjectRequest);

} else {
    Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
}
demo路径https://download.csdn.net/download/qq_39521922/10514791



你可能感兴趣的:(Android,图灵机器人,android)