zoukankan      html  css  js  c++  java
  • ndroid实现智能问答机器人(二) -----基于图灵机器人实现对话

    一、具体实现的代码

    1配置类,配置自己的图灵机器人(Config)(后期改成我们的后台为服务器的连接)

        public class Config {

            public static final String URL_KEY = "http://www.tuling123.com/openapi/api";

            public static final String APP_KEY = "38026ee35d614607b29c4ef3a56474a7";//此处是申请的Apikey

        }

    2.格式化日期时间的工具类,用于显示时间(DateUtils)

        import android.annotation.SuppressLint;

        import java.text.SimpleDateFormat;

        import java.util.Date;

         

        /**

         * 时间格式化工具类

         */

         

        public class DateUtils {

         

            @SuppressLint("SimpleDateFormat")

            public static String dateToString(Date date) {

                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");

                return df.format(date);

            }

        }

    3.HttpUtils网络请求类(HttpUtils)

        import com.google.gson.Gson;

         

        import java.io.ByteArrayOutputStream;

        import java.io.IOException;

        import java.io.InputStream;

        import java.io.UnsupportedEncodingException;

        import java.net.HttpURLConnection;

        import java.net.URL;

        import java.net.URLEncoder;

        import java.util.Date;

         

        /**

         * author:Created by ZhangPengFei.

         * data: 2017/12/28

         * http工具类

         */

        public class HttpUtils {

         

            /**

             * 发送消息到服务器

             * @return:消息对象

             */

            public static ChatMessage sendMessage(String message) {

                ChatMessage chatMessage = new ChatMessage();

                String gsonResult = doGet(message);

                Gson gson = new Gson();

                Result result = null;

                if (gsonResult != null) {

                    try {

                        result = gson.fromJson(gsonResult, Result.class);

                        chatMessage.setMessage(result.getText());

                    } catch (Exception e) {

                        chatMessage.setMessage("服务器繁忙,请稍候再试...");

                    }

                }

                chatMessage.setData(new Date());

                chatMessage.setType(ChatMessage.Type.INCOUNT);

                return chatMessage;

            }

         

            /**

             * get请求

             * @param message :发送的话

             * @return:数据

             */

            public static String doGet(String message) {

                String result = "";

                String url = setParmat(message);

                System.out.println("------------url = " + url);

                InputStream is = null;

                ByteArrayOutputStream baos = null;

                try {

                    URL urls = new URL(url);

                    HttpURLConnection connection = (HttpURLConnection) urls

                            .openConnection();

                    connection.setReadTimeout(5 * 1000);

                    connection.setConnectTimeout(5 * 1000);

                    connection.setRequestMethod("GET");

         

                    is = connection.getInputStream();

                    baos = new ByteArrayOutputStream();

                    int len = -1;

                    byte[] buff = new byte[1024];

                    while ((len = is.read(buff)) != -1) {

                        baos.write(buff, 0, len);

                    }

                    baos.flush();

                    result = new String(baos.toByteArray());

                } catch (Exception e) {

                    e.printStackTrace();

                } finally {

                    if (is != null) {

                        try {

                            is.close();

                        } catch (IOException e) {

                            e.printStackTrace();

                        }

                    }

         

                    if (baos != null) {

                        try {

                            baos.close();

                        } catch (IOException e) {

                            e.printStackTrace();

                        }

                    }

                }

                return result;

            }

         

            /**

             * 设置参数

             *

             * @param message : 信息

             * @return url

             */

            private static String setParmat(String message) {

                String url = "";

                try {

                    url = Config.URL_KEY + "?" + "key=" + Config.APP_KEY + "&info="

                            + URLEncoder.encode(message, "UTF-8");

                } catch (UnsupportedEncodingException e) {

                    e.printStackTrace();

                }

                return url;

            }

        }

    4.请求api地址返回的数据(Result)

        /**

         * 映射服务器返回的结果

         */

        public class Result {

         

            private int code; // code

            private String text; // 信息

         

            public int getCode() {

                return code;

            }

         

            public void setCode(int code) {

                this.code = code;

            }

         

            public String getText() {

                return text;

            }

         

            public void setText(String text) {

                this.text = text;

            }

         

        }

    10.聊天消息的实体类(ChatMessage)

        import java.util.Date;

         

        /**

         * 聊天消息的实体类

         */

        public class ChatMessage {

         

            private String name;// 姓名

            private String message;// 消息

            private Type type;// 类型:0.发送者 1.接受者

            private Date data;// 时间

         

            public ChatMessage() {

         

            }

         

            public ChatMessage(String message, Type type, Date data) {

                super();

                this.message = message;

                this.type = type;

                this.data = data;

            }

         

            public String getName() {

                return name;

            }

         

            public void setName(String name) {

                this.name = name;

            }

         

            public String getMessage() {

                return message;

            }

         

            public void setMessage(String message) {

                this.message = message;

            }

         

            public Type getType() {

                return type;

            }

         

            public void setType(Type type) {

                this.type = type;

            }

         

            public Date getData() {

                return data;

            }

         

            public void setData(Date data) {

                this.data = data;

            }

         

            public enum Type {

                INCOUNT, OUTCOUNT

            }

        }

    5.服务器发送与接收消息,左边布局的实现(layout_left)

        <?xml version="1.0" encoding="utf-8"?>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="vertical" >

         

            <TextView

                android:background="#f5f5f5"

                android:id="@+id/chat_left_time"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:paddingTop="5dp"

                android:textSize="14sp"

                android:text="2015/5/6   12:10:13" />

         

            <LinearLayout

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:orientation="horizontal" >

         

                <LinearLayout

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:orientation="vertical" >

         

                    <ImageView

                        android:id="@+id/chat_left_image"

                        android:layout_width="100dp"

                        android:layout_height="100dp"

                        android:src="@drawable/ser" />

         

                    <TextView

                        android:id="@+id/chat_left_name"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:layout_marginLeft="25dp"

                        android:text="小蜜"

                        android:textSize="16sp" />

                </LinearLayout>

         

                <TextView

                    android:layout_marginLeft="10dp"

                    android:background="@drawable/kefuborder"

                    android:gravity="center"

                    android:textSize="16sp"

                    android:layout_gravity="center_vertical"

                    android:id="@+id/chat_left_message"

                    android:layout_width="220dp"

                    android:layout_height="wrap_content"

                    android:text="您好。" />

            </LinearLayout>

         

        </LinearLayout>

    6.客户端发送与接收消息,右边布局的实现(layout_right)

        <?xml version="1.0" encoding="utf-8"?>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="vertical" >

         

            <TextView

                android:id="@+id/chat_right_time"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:background="#f5f5f5"

                android:paddingTop="5dp"

                android:text="2015/5/6   12:10:13"

                android:textSize="14sp" />

         

            <LinearLayout

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:gravity="right"

                android:orientation="horizontal" >

         

                <TextView

                    android:id="@+id/chat_right_message"

                    android:layout_width="220dp"

                    android:layout_height="wrap_content"

                    android:layout_gravity="center_vertical"

                    android:layout_marginRight="10dp"

                    android:background="@drawable/myborder"

                    android:gravity="center"

                    android:text="can i help me ?"

                    android:textSize="16sp" />

         

                <LinearLayout

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:orientation="vertical" >

         

                    <ImageView

                        android:id="@+id/chat_right_image"

                        android:layout_width="100dp"

                        android:layout_height="100dp"

                        android:src="@drawable/m" />

         

                    <TextView

                        android:id="@+id/chat_right_name"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:layout_marginLeft="25dp"

                        android:layout_marginTop="5dp"

                        android:text="zengtao"

                        android:textSize="16sp" />

                </LinearLayout>

            </LinearLayout>

         

        </LinearLayout>

    7.主界面聊天页面布局的实现(activity_chat)

        <?xml version="1.0" encoding="utf-8"?>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="@drawable/bac"

            android:orientation="vertical" >

         

            <!-- 头部 -->

         

            <RelativeLayout

                android:id="@+id/chat_top"

                android:layout_width="match_parent"

                android:layout_height="50dp"

                android:background="#3A4449" >

         

                <TextView

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_centerInParent="true"

                    android:text="小蜜"

                    android:textColor="#ffffff"

                    android:textSize="18sp" />

            </RelativeLayout>

         

            <!-- 底部 -->

         

            <RelativeLayout

                android:id="@+id/chat_bottom"

                android:layout_width="match_parent"

                android:layout_height="55dp"

                android:layout_alignParentBottom="true"

                android:background="#3A4449" >

         

                <EditText

                    android:id="@+id/chat_input_message"

                    android:layout_width="240dp"

                    android:background="@drawable/shuruborder"

                    android:layout_height="wrap_content"

                    android:layout_centerVertical="true"

                    android:layout_marginLeft="5dp"

                    android:gravity="center" />

         

                <Button

                    android:background="@drawable/btnborder"

                    android:id="@+id/chat_send"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_alignParentRight="true"

                    android:layout_centerVertical="true"

                    android:layout_toRightOf="@id/chat_input_message"

                    android:text="发送"

                    android:textColor="#FFFFFF"

                    android:textSize="18sp" />

            </RelativeLayout>

         

            <!-- 中间 -->

         

            <ListView

                android:id="@+id/chat_listview"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_above="@id/chat_bottom"

                android:layout_below="@id/chat_top"

                android:divider="@null"

                android:dividerHeight="3dp" >

            </ListView>

         

        </RelativeLayout>

    8.聊天消息的适配器(ChatMessageAdapter)

        /**

         * author:Created by ZhangPengFei.

         * data: 2017/12/28

         */

         

        import android.annotation.SuppressLint;

        import android.view.LayoutInflater;

        import android.view.View;

        import android.view.ViewGroup;

        import android.widget.BaseAdapter;

        import android.widget.TextView;

         

        import java.util.List;

         

        import weektest.project.R;

         

         

        /**

         * 聊天信息适配器

         *

          */

        public class ChatMessageAdapter extends BaseAdapter {

         

            private List<ChatMessage> list;

         

            public ChatMessageAdapter(List<ChatMessage> list) {

                this.list = list;

            }

         

            @Override

            public int getCount() {

                return list.isEmpty() ? 0 : list.size();

            }

         

            @Override

            public Object getItem(int position) {

                return list.get(position);

            }

         

            @Override

            public long getItemId(int position) {

                return position;

            }

         

            @Override

            public int getItemViewType(int position) {

                ChatMessage chatMessage = list.get(position);

                // 如果是接收消息:0,发送消息:1

                if (chatMessage.getType() == ChatMessage.Type.INCOUNT) {

                    return 0;

                }

                return 1;

            }

         

            @Override

            public int getViewTypeCount() {

                return 2;

            }

         

            @SuppressLint("InflateParams")

            @Override

            public View getView(int position, View convertView, ViewGroup parent) {

                ChatMessage chatMessage = list.get(position);

                if (convertView == null) {

                    ViewHolder viewHolder = null;

                    // 通过ItemType加载不同的布局

                    if (getItemViewType(position) == 0) {

                        convertView = LayoutInflater.from(parent.getContext()).inflate(

                                R.layout.layout_left, null);

                        viewHolder = new ViewHolder();

                        viewHolder.chat_time = (TextView) convertView

                                .findViewById(R.id.chat_left_time);

                        viewHolder.chat_message = (TextView) convertView

                                .findViewById(R.id.chat_left_message);

                    } else {

                        convertView = LayoutInflater.from(parent.getContext()).inflate(

                                R.layout.layout_right, null);

                        viewHolder = new ViewHolder();

                        viewHolder.chat_time = (TextView) convertView

                                .findViewById(R.id.chat_right_time);

                        viewHolder.chat_message = (TextView) convertView

                                .findViewById(R.id.chat_right_message);

                    }

                    convertView.setTag(viewHolder);

                }

                // 设置数据

                ViewHolder vh = (ViewHolder) convertView.getTag();

                vh.chat_time.setText(DateUtils.dateToString(chatMessage.getData()));

                vh.chat_message.setText(chatMessage.getMessage());

                return convertView;

            }

         

            /**

             * 内部类:只寻找一次控件

             *

             * @author zengtao 201556日 下午2:27:57

             */

            private class ViewHolder {

                private TextView chat_time, chat_message;

            }

        }

    9.java的实现(ChatActivity)

        import android.annotation.SuppressLint;

        import android.app.Activity;

        import android.os.Bundle;

        import android.os.Handler;

        import android.os.Message;

        import android.text.TextUtils;

        import android.view.View;

        import android.view.View.OnClickListener;

        import android.view.Window;

        import android.widget.Button;

        import android.widget.EditText;

        import android.widget.ListView;

        import android.widget.Toast;

         

        import java.util.ArrayList;

        import java.util.Date;

        import java.util.List;

         

        import weektest.project.R;

         

        public class ChatActivity extends Activity {

            private List<ChatMessage> list;

            private ListView chat_listview;

            private EditText chat_input;

            private Button chat_send;

            private ChatMessageAdapter chatAdapter;

            private ChatMessage chatMessage = null;

         

            @Override

            protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                requestWindowFeature(Window.FEATURE_NO_TITLE);

                setContentView(R.layout.activity_chat);

                initView();

                initListener();

                initData();

            }

         

            // 1.初始试图

            private void initView() {

                // 1.初始化

                chat_listview = (ListView) findViewById(R.id.chat_listview);

                chat_input = (EditText) findViewById(R.id.chat_input_message);

                chat_send = (Button) findViewById(R.id.chat_send);

            }

         

            // 2.设置监听事件

            private void initListener() {

                chat_send.setOnClickListener(onClickListener);

            }

         

            // 3.初始化数据

            private void initData() {

                list = new ArrayList<ChatMessage>();

                list.add(new ChatMessage("您好,小乖为您服务!", ChatMessage.Type.INCOUNT, new Date()));

                chatAdapter = new ChatMessageAdapter(list);

                chat_listview.setAdapter(chatAdapter);

                chatAdapter.notifyDataSetChanged();

            }

         

            // 4.发送消息聊天

            private void chat() {

                // 1.判断是否输入内容

                final String send_message = chat_input.getText().toString().trim();

                if (TextUtils.isEmpty(send_message)) {

                    Toast.makeText(ChatActivity.this, "对不起,您还未发送任何消息",

                            Toast.LENGTH_SHORT).show();

                    return;

                }

         

                // 2.自己输入的内容也是一条记录,记录刷新

                ChatMessage sendChatMessage = new ChatMessage();

                sendChatMessage.setMessage(send_message);

                sendChatMessage.setData(new Date());

                sendChatMessage.setType(ChatMessage.Type.OUTCOUNT);

                list.add(sendChatMessage);

                chatAdapter.notifyDataSetChanged();

                chat_input.setText("");

         

                // 3.发送你的消息,去服务器端,返回数据

                new Thread() {

                    public void run() {

                        ChatMessage chat = HttpUtils.sendMessage(send_message);

                        Message message = new Message();

                        message.what = 0x1;

                        message.obj = chat;

                        handler.sendMessage(message);

                    };

                }.start();

            }

         

            @SuppressLint("HandlerLeak")

            private Handler handler = new Handler() {

         

                public void handleMessage(android.os.Message msg) {

                    if (msg.what == 0x1) {

                        if (msg.obj != null) {

                            chatMessage = (ChatMessage) msg.obj;

                        }

                        // 添加数据到list中,更新数据

                        list.add(chatMessage);

                        chatAdapter.notifyDataSetChanged();

                    }

                };

            };

         

            // 点击事件监听

            OnClickListener onClickListener = new OnClickListener() {

         

                @Override

                public void onClick(View v) {

                    switch (v.getId()) {

                        case R.id.chat_send:

                            chat();

                            break;

                    }

                }

            };

        }

    10.权限与依赖问题

       <uses-permission android:name="android.permission.INTERNET" /> <!-- 网络权限 -->

    compile 'com.google.code.gson:gson:2.2.4'//Gson解析依赖

    18.写了这么多,就把图片和绘制的形状一块给你们吧,

    .输入框的样式(shuruborder)

        <?xml version="1.0" encoding="utf-8"?>

        <shape xmlns:android="http://schemas.android.com/apk/res/android">

         

            <stroke

                android:width="1dp"

                android:color="#FFF" />

            <solid android:color="#FFF" />

            <corners android:radius="5dip" />

         

        </shape>

    .聊天框的样式(kefuborder)

        <?xml version="1.0" encoding="utf-8"?>

        <shape xmlns:android="http://schemas.android.com/apk/res/android">

         

            <stroke

                android:width="1dp"

                android:color="#5188DE" />

            <solid android:color="#A5D932" />

            <corners android:radius="8dip" />

         

        </shape>

    .自己聊天框的样式(myborder)

        <?xml version="1.0" encoding="utf-8"?>

        <shape xmlns:android="http://schemas.android.com/apk/res/android">

         

            <stroke

                android:width="1dp"

                android:color="#787878" />

            <solid android:color="#FFFFFF" />

            <corners android:radius="8dip" />

         

        </shape>

    .发送按钮的样式(btnborder)

        <?xml version="1.0" encoding="utf-8"?>

        <shape xmlns:android="http://schemas.android.com/apk/res/android">

         

            <solid android:color="#CCCCCC"/>

            <corners android:radius="5dip"/>

         

    </shape>

    二、效果图

  • 相关阅读:
    抽象工厂模式
    外观模式
    策略模式
    状态模式
    观察者模式
    装饰者模式
    模板方法模式
    适配器模式
    中介者模式
    组合模式
  • 原文地址:https://www.cnblogs.com/jingxiaopu/p/9310444.html
Copyright © 2011-2022 走看看