Android使用图灵机器人实现防QQ聊天(volley框架)
去图灵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<JSONObject>() { @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<String, Object> 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.****.net/download/qq_39521922/10514791