智能聊天机器人

项目名称:智能聊天机器人

开发语言:java

开发环境:Eclipse、android SDK

项目简介:一款实现智能机器人聊天的软件

1. 用户平台接入

要编写属于自己的智能聊天机器人,必须在图灵机器人官网上注册一个账号,获取IDKey还有你必须知道需要传递的一系列参数

          

2. 界面显示

因为有发送消息和接收消息两种状态,所以界面显示就是两种,需要定义一个标识,判断是发送消息还是接收消息

这里是因为模拟器的原因不能输中文,但是在手机上测试过是可以的

 

package com.example.weixin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.weixin.ChatMessage.Type;
import com.example.weixin.ConnectionUtils.CallConnectionInterface;
import com.example.weixin.ConnectionUtils.Method;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements
		android.view.View.OnClickListener {
	private String APIKEY = "0c0118dc7ce1cf10016ea6a7997da87f";
	private String USERID = "119971";
	private String URL = "http://www.tuling123.com/openapi/api";

	private ListView mListView;
	private EditText mMsg;
	private Button mButton;
	private List<ChatMessage> mDatas = new ArrayList<ChatMessage>();
	private ChatMessageAdapter mAdapter;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			ChatMessage message1 = (ChatMessage) msg.obj;
			mDatas.add(message1);
			mAdapter.notifyDataSetChanged();
			mListView.setSelection(mDatas.size() - 1);
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();

		mAdapter = new ChatMessageAdapter(this, mDatas);
		mListView.setAdapter(mAdapter);
		mButton.setOnClickListener(this);

	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.message_list);
		mMsg = (EditText) findViewById(R.id.message_edit);
		mButton = (Button) findViewById(R.id.send_message_btn);
		mDatas.add(new ChatMessage(Type.INPUT, "我是图灵,很高兴为您服务"));

	}

	public void sendMessage() {
		final String msg = mMsg.getText().toString();
		if (TextUtils.isEmpty(msg)) {
			Toast.makeText(this, "您还没有填写信息呢...", Toast.LENGTH_SHORT).show();
			return;
		}
		ChatMessage to = new ChatMessage(Type.OUTPUT, msg);
		to.setDate(new Date());
		mDatas.add(to);
		mAdapter.notifyDataSetChanged();
		mListView.setSelection(mDatas.size() - 1);
		mMsg.setText("");

		// 关闭软键盘
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		// 得到InputMethodManager的实例
		if (imm.isActive()) {
			// 如果开启
			imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
					InputMethodManager.HIDE_NOT_ALWAYS);
			// 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
		}

		ConnectionUtils ConnectionUtils = new ConnectionUtils();
		HashMap<String, String> parameterMap = new HashMap<String, String>();
		parameterMap.put("key", APIKEY);
		parameterMap.put("info", msg);
		parameterMap.put("userid", USERID);

		ConnectionUtils.asyncTaskConnection(URL, parameterMap, Method.GET,
				new CallConnectionInterface() {

					@Override
					public void executeResult(String result) {
						JSONObject jsonobject;
						try {
							jsonobject = new JSONObject(result);
							int code = jsonobject.getInt("code");
							String text = jsonobject.getString("text");
							ChatMessage message = new ChatMessage();
							if (code > 400000 || text == null
									|| text.trim().equals("")) {
								message.setMsg("该功能等待开发...");
							} else {
								message.setMsg(text);
							}
							message.setType(Type.INPUT);
							message.setDate(new Date());
							Message message1 = Message.obtain();
							message1.obj = message;
							mHandler.sendMessage(message1);
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});

	}

	@Override
	public void onClick(View v) {
		sendMessage();

	}

}


这里适配器有点关键

package com.example.weixin;

import java.util.List;

import com.example.weixin.ChatMessage.Type;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ChatMessageAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<ChatMessage> mDatas;

	public ChatMessageAdapter(Context context, List<ChatMessage> datas) {
		mInflater = LayoutInflater.from(context);
		mDatas = datas;
	}

	@Override
	public int getCount() {

		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {

		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	/**
	 * 接受到消息为1,发送消息为0
	 */
	@Override
	public int getItemViewType(int position) {
		ChatMessage msg = mDatas.get(position);
		return msg.getType() == Type.INPUT ? 1 : 0;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ChatMessage chatMessage = mDatas.get(position);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			if (chatMessage.getType() == Type.INPUT) {
				convertView = mInflater.inflate(
						R.layout.chatting_item_msg_text_left, null);
				viewHolder.createDate = (TextView) convertView
						.findViewById(R.id.from_sendtime);
				viewHolder.content = (TextView) convertView
						.findViewById(R.id.from_chatcontent);
				convertView.setTag(viewHolder);

			} else {
				convertView = mInflater.inflate(
						R.layout.chatting_item_msg_text_right, null);
				viewHolder.createDate = (TextView) convertView
						.findViewById(R.id.tv_sendtime);
				viewHolder.content = (TextView) convertView
						.findViewById(R.id.tv_chatcontent);
				convertView.setTag(viewHolder);
			}

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.content.setText(chatMessage.getMsg());
		viewHolder.createDate.setText(chatMessage.getDateStr());
		return convertView;
	}

	private class ViewHolder {
		public TextView createDate;
		public TextView name;
		public TextView content;
	}

}


自定义的类

package com.example.weixin;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChatMessage {
	/*
	 * 消息类型
	 */
	private Type type;
	/*
	 * 消息内容
	 */
	private String msg;
	/*
	 * 日期
	 */
	private Date date;
	/*
	 * 日期的字符串格式
	 */
	private String dateStr;
	/*
	 * 发送人
	 */
	private String name;

	public enum Type {
		INPUT, OUTPUT
	}

	public ChatMessage() {

	}

	public ChatMessage(Type type, String msg) {
		super();
		this.type = type;
		this.msg = msg;
		setDate(new Date());
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getDateStr() {
		return dateStr;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDate() {
		return date;
	}

	void setDate(Date date) {
		this.date = date;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.dateStr = df.format(date);
	}

}


 

你可能感兴趣的:(智能聊天机器人)