• 简易的聊天界面以及聊天机器人的实现


    现在的聊天机器人一般都是通过连接网络API接口实现的,通过对接口的网络访问,实现聊天数据的传输,我这个就是访问图灵机器人的接口实现。

    先看效果图:
    在这里插入图片描述

    界面代码:

    1.activity_main.xml主界面布局、包括头部及一个listview和下方的文本输入框与按钮。

    xmlns:tools="http://schemas.android.com/tools"
    
    android:id="@+id/activity_main"
    
    android:layout_width="match_parent"
    
    android:layout_height="match_parent"
    
    android:orientation="vertical"
    
    tools:context="com.jju.edu.robot.MainActivity">
    
    
    
    
    
    
    
        
    
        

    2.title.xml头部的详细布局里面有左、中、右三个textview,方便代码重复调用(这项目里是没必要的,习惯了!)这里只用到了中间的textview。

    android:orientation="vertical"
    
    android:layout_width="match_parent"
    
    android:layout_height="50dp">
    
    
    
        
    
        
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    3.这里是listview里面item的两种布局,分别是我们发送信息的显示布局以及收到信息的显示布局。

    list_item_left.xml

    android:orientation="vertical"  
    
    android:layout_width="match_parent"
    
    android:layout_height="wrap_content">
    
    
    
        
    
        
    
            
    
                
    
                
    
            
    
            
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    list_item_right.xml

    android:layout_width="match_parent"
    
    android:layout_height="wrap_content"
    
    android:orientation="vertical">
    
    
    
        
    
        
    
            
    
                
    
                
    
            
    
            
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    Java代码:

    首先得先找到图灵机器人的API接口,该接口有很多数据平台都会提供,而且完全免费!我这里调用的是聚合数据里的,就以聚合数据的接口示例。

    注册后登录,实名认证完成后直接申请数据即可。申请成功,会获得key值及申请示例。下面直接看代码,注释里会解释:

    1.MainActivity.class

    package com.jju.edu.robot;

    import android.app.Activity;

    import android.os.Handler;

    import android.os.Message;

    import android.os.Bundle;

    import android.util.Log;

    import android.view.View;

    import android.view.Window;

    import android.widget.EditText;

    import android.widget.ListView;

    import android.widget.TextView;

    import android.widget.Toast;

    import org.json.JSONObject;

    import java.io.InputStream;

    import java.net.HttpURLConnection;

    import java.net.URL;

    import java.net.URLEncoder;

    import java.util.ArrayList;

    import java.util.Calendar;

    import java.util.List;

    public class MainActivity extends Activity {

    private String time;//时间(会在每次发送或接收数据时重新赋值)
    
    private String path1 = "http://op.juhe.cn/robot/index?info=";//接口路径
    
    private String path2 = "&dtype=&loc=&lon=&lat=&userid=&key=8a2e2e11c3453a5fd3002b864174e440";//发送的各种参数及key值(这里就不过多设置参数了,直接默认)
    
    private String text = "";
    
    private TextView middle;
    
    private EditText ed_message;
    
    private List list = new ArrayList();
    
    private MyAdapter adapter;
    
    private ListView list_view;
    
    @Override
    
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    
        setContentView(R.layout.activity_main);
    
        //获取控件
    
        middle = (TextView) findViewById(R.id.middle);
    
        ed_message = (EditText) findViewById(R.id.ed_message);
    
        list_view = (ListView) findViewById(R.id.list_view);
    
        list_view.setDividerHeight(0);//去掉黑线
    
        middle.setText("聊天界面");//设置头部标题
    
        //将初始值放入list集合
    
        MessageUtil util = new MessageUtil();
    
        util.setJudge(false);
    
        util.setTime(getTime());
    
        util.setMessage("您好!我是小灵!");
    
        list.add(util);
    
        //定义自定义适配器并赋值
    
        adapter = new MyAdapter(MainActivity.this, list);
    
        //将自定义适配器添加到listview
    
        list_view.setAdapter(adapter);
    
    }
    
    //获取时间,每次调用都会获得调用时的当前时间。
    
    public String getTime() {
    
        Calendar c = Calendar.getInstance();
    
        time = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
    
        Log.e("时间", time);
    
        return time;
    
    }
    
    //按钮点击事件
    
    public void submit(View view) {
    
        String message = ed_message.getText().toString();
    
        //输入不能为空
    
        if (message == null || message.equals("")) {
    
            Toast.makeText(MainActivity.this, "输入不能为空!", Toast.LENGTH_SHORT).show();
    
        } else {
    
            //添加自己发送的数据到list集合
    
            MessageUtil util = new MessageUtil();
    
            util.setJudge(true);
    
            util.setTime(getTime());
    
            util.setMessage(message);
    
            list.add(util);
    
            //添加改变值
    
            adapter.notifyDataSetChanged();
    
            //显示listview最后一个值
    
            list_view.setSelection(list.size());
    
            //网络操作
    
            http_(message);
    
        }
    
        ed_message.setText("");
    
    }
    
    //网络访问操作
    
    public void http_(final String string) {
    
        new Thread() {
    
            @Override
    
            public void run() {
    
                try {
    
                    //保证编码格式正确
    
                    String city = URLEncoder.encode(string, "UTF-8");
    
                    //拼接地址与参数
    
                    URL url = new URL(path1 + city + path2);
    
                    //访问
    
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
                    connection.connect();
    
                    InputStream is = connection.getInputStream();
    
                    //获得返回值
    
                    String info = getInfo(is);
    
                    Log.e("****", info);
    
                    //子线程无法操作UI,所以讲数据传递给handler。
    
                    Message message = handler.obtainMessage();
    
                    message.obj = info;
    
                    message.what = 123;
    
                    handler.sendMessage(message);
    
                } catch (Exception e) {
    
                    e.printStackTrace();
    
                }
    
            }
    
        }.start();
    
    }
    
    //获得返回数据,调用解析方法
    
    public Handler handler = new Handler() {
    
        @Override
    
        public void handleMessage(Message msg) {
    
            if (msg.what == 123) {
    
                text = (String) msg.obj;
    
                json(text);
    
            }
    
        }
    
    };
    
    //读取返回数据将其转换成String型并返回
    
    public String getInfo(InputStream is) {
    
        String info = null;
    
        byte[] by = new byte[1024];
    
        StringBuilder builder = new StringBuilder();
    
        int count = -1;
    
        try {
    
            while ((count = is.read(by)) != -1) {
    
                builder.append(new String(by, 0, count));
    
            }
    
            info = builder.toString();
    
            is.close();
    
        } catch (Exception e) {
    
            e.printStackTrace();
    
        }
    
        return info;
    
    }
    
    //解析返回的jsons数据,并将值添加到list集合后保存添加数据(应该将解析方法与添加list集合的方法写成两个方法的,这里就懒得分开了)
    
    public void json(String string) {
    
        try {
    
            JSONObject object = new JSONObject(string);
    
            String back = object.getString("result");
    
            JSONObject object1 = new JSONObject(back);
    
            String back_message = object1.getString("text");
    
            MessageUtil util = new MessageUtil();
    
            util.setJudge(false);
    
            util.setTime(getTime());
    
            util.setMessage(back_message);
    
            list.add(util);
    
            adapter.notifyDataSetChanged();
    
            list_view.setSelection(list.size());
    
        } catch (Exception e) {
    
            e.printStackTrace();
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265

    }

    2.MessageUtil.class 构造方法类

    package com.jju.edu.robot;

    /**

    • Created by LingHao on 2016/11/3.

    • 构造方法类

    */

    public class MessageUtil {

    private String time;
    
    private String message;
    
    private boolean judge;
    
    public String getTime() {
    
        return time;
    
    }
    
    public void setTime(String time) {
    
        this.time = time;
    
    }
    
    public boolean isJudge() {
    
        return judge;
    
    }
    
    public void setJudge(boolean judge) {
    
        this.judge = judge;
    
    }
    
    public String getMessage() {
    
        return message;
    
    }
    
    public void setMessage(String message) {
    
        this.message = message;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    }

    3.MyAdapter.class 自定义适配器类。

    package com.jju.edu.robot;

    import android.content.Context;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.BaseAdapter;

    import android.widget.TextView;

    import java.util.List;

    /**

    • Created by LingHao on 2016/11/3.

    */

    public class MyAdapter extends BaseAdapter {

    private Context context;
    
    private List list;
    
    private LayoutInflater inflater;
    
    private int COME_MSG = 0;
    
    private int TO_MSG = 1;
    
    public MyAdapter(Context context, List list) {
    
        this.context = context;
    
        this.list = list;
    
        inflater = LayoutInflater.from(context);
    
    }
    
    @Override
    
    public int getCount() {
    
        return list.size();
    
    }
    
    @Override
    
    public Object getItem(int position) {
    
        return list.get(position);
    
    }
    
    @Override
    
    public long getItemId(int position) {
    
        return position;
    
    }
    
    @Override
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
        //缓存机制
    
        ViewHolder holder = null;
    
        if (convertView == null) {
    
            holder = new ViewHolder();
    
            //判断是发送还是接收,从而设置风格样式靠左还是靠右
    
            if (list.get(position).isJudge()) {
    
                convertView = inflater.inflate(R.layout.list_item_right, null);
    
            } else {
    
                convertView = inflater.inflate(R.layout.list_item_left, null);
    
            }
    
            holder.time = (TextView) convertView.findViewById(R.id.time);
    
            holder.content = (TextView) convertView.findViewById(R.id.content);
    
            convertView.setTag(holder);
    
        } else {
    
            holder = (ViewHolder) convertView.getTag();
    
        }
    
        holder.time.setText(list.get(position).getTime());
    
        holder.content.setText(list.get(position).getMessage());
    
        return convertView;
    
    }
    
    class ViewHolder {
    
        TextView time, content;
    
    }
    
    @Override
    
    public int getItemViewType(int position) {
    
        // 区别两种view的类型,标注两个不同的变量来分别表示各自的类型
    
        MessageUtil util = list.get(position);
    
        if (util.isJudge()) {
    
            return COME_MSG;
    
        } else {
    
            return TO_MSG;
    
        }
    
    }
    
    @Override
    
    public int getViewTypeCount() {
    
        // 这个方法默认返回1,如果希望listview的item都是一样的就返回1,我们这里有两种风格,返回2
    
        return 2;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123

    }

    注意:运行之前要添加网络权限:

    文章来源:网络 版权归原作者所有

    上文内容不用于商业目的,如涉及知识产权问题,请权利人联系小编,我们将立即处理

  • 相关阅读:
    Afterlogic WebMail Pro PHP Crack
    Qt Quick 用cmake怎么玩子项目
    大数据在电力行业的应用案例100讲(二十六)-分布式ID微服务实现及应用
    【python】使用python操作XML文件及JSON与XML互转
    常见的近似算法
    容器-基础
    整合:qlib的AI导向的框架与backtrader的事件驱动与实盘
    从零学习开发一个RISC-V操作系统(三)丨嵌入式操作系统开发的常用概念和工具
    SpringSecurity入门到精通
    Flink SQL: SET Statements
  • 原文地址:https://blog.csdn.net/xuezhangmen/article/details/127664883