实现微信中的聊天界面Demo

本文将实现一个仿微信的聊天界面demo,对话的双方分别来自“自己”和第三方接口“图灵机器人”,该接口会根据您发送的信息智能回答,从而形成简单的对话聊天。效果如下:
这里写图片描述


界面分析:

  • 聊天界面整体上是一个ListView,该ListView的item包含了两种布局,每种布局都包含时间、聊天内容、和布局样式(区分是哪种布局)。

  • 当自己发送一个内容时,将该内容添加到Adapter所绑定的List中,并刷新Adapter;再将List中的内容拼接到接口中远程访问Url,返回结果是一个JSON格式的数据,使用GSON将其解析,并将内容部分添加到List中,再次通知Adapter刷新List,这样就实现了对话。由于远程访问Url是一个网络操作,故不可将其放在主线程中,又由于返回的数据需要在添加到List中再通知Adapter刷新,这是一个主线程操作。由于涉及到线程见的交互,所以需要使用Handler控制。


(本demo所需的资源图片地址:http://img.mukewang.com/down/54586b4c0001ec8100000000.zip


下面是代码解析:

  • 首先您需要在图灵网(www.tuling123.com)注册一个账号,并使用该账号绑定您要编写的应用程序,图灵网会返回一个改程序对应的API Key。
  • 使用GET请求访问http://www.tuling123.com/openapi/api地址以获取返回的JSON格式的数据,还需要在GET请求中添加”key”(也就是申请的API Key),”info”(就是您的发送的信息),”userid”(表示用户的位移标识,确保唯一性)。

    下面使用一个工具类来封装该GET请求:

public class Utils {
   
    private static final String URL = "http://www.tuling123.com/openapi/api";
    private static final String API_KEY = "6bb5146414bb4fd9a8cf192be580d367";

    /**
     * @param msg 发送的内容
     * @return 远程返回的JSON格式的字符串
     * @throws IOException
     */
    public static String doGet(String msg) throws IOException {
        String result = "";
        String url = getUrl(msg);
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            java.net.URL urlConn = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlConn.openConnection();
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            is = conn.getInputStream();
            int len = -1;
            byte[] byteRead = new byte[128];
            baos = new ByteArrayOutputStream();

            while ((len = is.read(byteRead)) != -1) {
                baos.write(byteRead, 0, len);

            }
            baos.flush();
            result = new String(baos.toByteArray());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (baos != null) {
                baos.close();

            }
        }
        return result;


    }

    /**
     * @param msg 发送的内容
     * @return 拼接的URL
     */
    private static String getUrl(String msg) {
        String result = null;
        try {
            result = URL + "?key=" + API_KEY + "&info=" + URLEncoder.encode(msg, "UTF-8") + "&userid=" + "12345678";

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发送一个消息 返回消息
     *
     * @param msg 发送的内容
     * @return 从JSON数据中去除的返回内容
     */
    public static MsgBean getMessage(String msg)

    {
        MsgBean msgBean = new MsgBean();
        try {
            String jsonString = doGet(msg);
            Gson gson = new Gson();
            Result result = gson.fromJson(jsonString, Result.class);
            msgBean.setMsg(result.getText());
        } catch (IOException e) {
            e.printStackTrace();
        }
        msgBean.setDate(new Date());
        msgBean.setType(MsgBean.Type.INCOMING);


        return msgBean;
    }

}

其中Result类是远程返回的JSON格式的数据,该类如下:

public class Result {
    private int 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;
    }
}

code为10000时表示请求成功,text即为结果。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值