首页>代码>Android客户端智能聊天图灵机器人demo>/TuLingDemo/src/com/stone/tulingdemo/MainActivity.java
package com.stone.tulingdemo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity implements HttpGetDatalistener,
		OnClickListener {

	private HttpData httpData;
	private List<ListData> listDatas;
	private ListView lv;
	private EditText sendtext;
	private Button send_btn;
	private String contentstr;//获取发送的内容
	private MyAdapter adapter;
	private String[] welcome_array;
	private double currenttime,oldtime=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	// 初始化
	private void initView() {
		listDatas = new ArrayList<ListData>();
		lv = (ListView) super.findViewById(R.id.lv);
		sendtext = (EditText) super.findViewById(R.id.sendText);
		send_btn = (Button) super.findViewById(R.id.send_btn);
		send_btn.setOnClickListener(this);
		adapter = new MyAdapter(listDatas, this);
		lv.setAdapter(adapter);
		ListData listData ;
		listData = new ListData(getRandomWelcomeIconeTips(), ListData.RECEIVE,getTime());
		listDatas.add(listData);
	}
	
	//获取自己添加的数据
	private String getRandomWelcomeIconeTips(){
		String welcome_tip = null;
		welcome_array = this.getResources().getStringArray(R.array.welcome_tips);
		int index = (int) (Math.random()*(welcome_array.length-1));
		welcome_tip = welcome_array[index];
		return welcome_tip;
	}

	@Override
	public void getDataUrl(String data) {
		// System.out.println(data);
		parseText(data);
	}

	// 解析json文字
	public void parseText(String str) {
		try {
			JSONObject jsonObject = new JSONObject(str);
			//System.out.println(jsonObject.getString("code"));
			//System.out.println(jsonObject.getString("text"));
			ListData listData = null;
			if(jsonObject.getString("code").equals("100000")){//文字类
				listData = new ListData(jsonObject.getString("text"),ListData.RECEIVE,getTime());
				listDatas.add(listData);
			}else{//非文字类
				switch (Integer.parseInt(jsonObject.getString("code"))) {
				case 200000:
					listData = new ListData(jsonObject.getString("text"),jsonObject.getString("url"),ListData.RECEIVE,getTime());
					break;//链接类

				default:
					break;
				}
				listDatas.add(listData);
			}
			adapter.notifyDataSetChanged();//重新适配
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	@Override
	public void onClick(View view) {
		contentstr = sendtext.getText().toString();	
		sendtext.setText("");
		ListData listdata ;
	    String dropk = contentstr.replace(" ", "");//去掉空格
	    String droph =dropk.replace("\n", "");//去掉换行
		listdata = new ListData(contentstr,ListData.SEND,getTime());
		listDatas.add(listdata);
		if(listDatas.size() > 30){
			for (int i = 0; i < 10; i++) {
				listDatas.remove(0);
			}
		}
		adapter.notifyDataSetChanged();
		httpData = (HttpData) new HttpData(
				"http://www.tuling123.com/openapi/api?key=4cb647e6b60fdd9933eba0e8e0330f86&info="+droph,
				this).execute();// 请求事件
	}
	
	//获取时间
	private String getTime(){
		currenttime = System.currentTimeMillis();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		Date curData = new Date();
		String str = dateFormat.format(curData);
		if(currenttime - oldtime > 5*60*1000 ){
			oldtime = currenttime;
			return str;
		}else{
			return "";
		}
	}

}
最近下载更多
GZW012345  LV8 2022年3月27日
李秋天  LV6 2021年12月21日
3089559272  LV11 2021年12月21日
fflzbb  LV1 2021年11月17日
iRichard1314  LV6 2021年4月22日
fengxiao  LV8 2020年12月29日
a566566  LV9 2020年12月27日
aaa5849310  LV25 2020年12月19日
sfdasfda  LV1 2020年12月13日
natism  LV9 2020年12月3日
最近浏览更多
3334004690  LV3 3月20日
学习112  LV2 2023年12月21日
hhhhhz  LV7 2023年11月14日
微信网友_6627705560322048  LV1 2023年8月30日
万紫怡  LV4 2023年6月15日
200171  LV9 2023年6月1日
202105013142  LV1 2023年5月2日
ajdgqd  LV3 2023年3月10日
annazhang  LV29 2023年2月17日
machunming  LV1 2023年2月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友