接入图灵机器人实现XMPP
来源:互联网 发布:哪里能找到工资数据 编辑:程序博客网 时间:2024/04/30 09:09
接入图灵机器人实现XMPP
转载请注明出处
- 注册图灵帐号密码,得到key
- 逻辑处理
1.注册图灵信息。
网站地址: [ 图灵 ]
2.代码展示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.yudehai.text.SecondActivity" > <TextView android:layout_width="match_parent" android:layout_height="50dip" android:textSize="20sp" android:gravity="center" android:text="客服美眉" android:background="#00ff00" /> <ListView android:id="@+id/listView" android:layout_marginTop="50dip" android:layout_marginBottom="50dip" android:layout_width="match_parent" android:layout_height="match_parent" android:stackFromBottom="true" ></ListView> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dip" android:weightSum="4" > <EditText android:id="@+id/message" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="3" android:ems="8" /> <Button android:id="@+id/send" android:layout_width="0dp" android:layout_height="match_parent" android:text="發送" android:layout_weight="1" android:gravity="center" /> </LinearLayout></RelativeLayout>
其实也就是一个ListView 属性android:stackFromBottom=”true” 意思是从底部向上开始展示数据
public class SecondActivity extends Activity implements OnCallListener{ /*** * 图灵key * 替换为你自己的图灵Key */ private static final String TULING_APPKEY=""; private static final String Pre = "http://www.tuling123.com/openapi/api?key="; private ListView list; private Button send; private EditText edit; private List<String> lists; private MyAdapter adapter; private ToastUtils pro; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); pro = new ToastUtils(this); list = (ListView) findViewById(R.id.listView); lists = new ArrayList<String>(); adapter = new MyAdapter(this,lists); list.setAdapter(adapter); send = (Button) findViewById(R.id.send); edit = (EditText) findViewById(R.id.message); send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(edit.getText().toString())){ Toast.makeText(SecondActivity.this, "不可爲空", Toast.LENGTH_SHORT).show(); }else{ pro.showunclickToastAlong(); lists.add(edit.getText().toString()); adapter.notifyDataSetChanged(); //KEY&info=你漂亮么 new JsonAsyncTask(SecondActivity.this).execute(Pre+TULING_APPKEY+"&info="+edit.getText().toString()); View softview = SecondActivity.this.getWindow().peekDecorView(); if (softview != null && softview.getWindowToken() != null) { InputMethodManager inputmanger = (InputMethodManager) SecondActivity.this .getSystemService(MainActivity.INPUT_METHOD_SERVICE); inputmanger.hideSoftInputFromWindow(softview.getWindowToken(), 0); } edit.setText(""); } } }); } @Override public void getMessage(String str) { pro.cancel(); if(TextUtils.isEmpty(str)){ lists.add("没网了,点你妹?"); }else{ lists.add(str); } adapter.notifyDataSetChanged(); list.setSelection(lists.size()-1); }}
代码其实很简单,只不过就是需要定义当返回为空或者访问失败自动填写一个数据,一行添加一行么,你懂的。解析用的是AsyncTask 详细了解请看AsyncTask详解
TULING_APPKEY只需要将这个key值填写成自己的key ,Pre的
顺便贡献一个仿苹果progress先上张效果图
代码其实也就是一个工具类
public class ToastProUtils {
private static ToastProUtils instanse = null;
private static Dialog dialog;
private static View view;
private TextView tv;
public synchronized static ToastProUtils getInstanse(Context context) { if (instanse == null) { instanse = new ToastProUtils(); } dialog = new Dialog(context, R.style.MyDialogStyle); view = LayoutInflater.from(context).inflate(R.layout.progressbar_item, null); dialog.setContentView(view); ImageView progressImageView = (ImageView) view .findViewById(R.id.myloading_image_id); AnimationDrawable animationDrawable = (AnimationDrawable) progressImageView .getDrawable(); animationDrawable.start(); WindowManager wm = ((Activity) context).getWindowManager(); Display display = wm.getDefaultDisplay(); android.view.WindowManager.LayoutParams lp = dialog.getWindow() .getAttributes(); lp.width = display.getWidth() / 3; lp.height = LayoutParams.WRAP_CONTENT; dialog.getWindow().setAttributes(lp); return instanse;}private ToastProUtils() {}/** * 方法说明: * */public void show() { tv = null; tv = (TextView) view.findViewById(R.id.mylaodint_text_id); tv.setText("加载中..."); if (dialog.isShowing()) { dialog.cancel(); dialog.show(); } dialog.show();}public void cancelKey(final boolean cancel){ dialog.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) { if (arg1 == KeyEvent.KEYCODE_BACK){ if(cancel) return false; else return true; } return false; } });}public void unclickshow() { if (dialog.isShowing()) { dialog.cancel(); dialog.setCanceledOnTouchOutside(false); dialog.show(); } else { dialog.setCanceledOnTouchOutside(false); dialog.show(); }}public void showstr(String str) { tv = null; tv = (TextView) view.findViewById(R.id.mylaodint_text_id); tv.setText(str); if (dialog.isShowing()) { dialog.cancel(); dialog.show(); } dialog.show();}public void unclickshowstr(String str) { tv = null; tv = (TextView) view.findViewById(R.id.mylaodint_text_id); tv.setText(str); if(dialog.isShowing()){ dialog.cancel(); dialog.setCanceledOnTouchOutside(false); dialog.show(); } else { dialog.setCanceledOnTouchOutside(false); dialog.show(); }}public void cancle() { if (dialog == null) { return; } else { dialog.cancel(); tv =null; }};public void setStr(String str){ tv = null; tv = (TextView) view.findViewById(R.id.mylaodint_text_id); tv.setText(str);}
}
public class ToastUtils { private static final String TAG = "ToastUtils"; private static Context mContext; private static String str = ""; private ToastProUtils progress=null; public ToastUtils(Context context, String str) { if(progress==null){ this.mContext = context; this.str = str; progress = ToastProUtils.getInstanse(context); } } public ToastUtils(Context context) { if(progress==null){ this.mContext = context; progress = ToastProUtils.getInstanse(context); } } public void cancel() { if(progress!=null){ progress.cancle(); } str=""; } public void showToastAlong() { if (progress != null) { if ("".equals(str)) { progress.show(); } else { progress.showstr(str); } } else { return; } } /*** * * 方法说明:进度显示 * * @param str */ public void setStr(String str){ if (progress != null) { if (!TextUtils.isEmpty(str)) { progress.setStr(str); } else { } } else { return; } } /*** * * 方法说明:不可点击的Show * */ public void showunclickToastAlong() { if (progress != null) { if ("".equals(str)) { progress.unclickshow(); } else { progress.unclickshowstr(str); } } else { return; } } public void showToastAlong(long delay) { showToastAlong(); Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { ToastUtils.this.cancel(); } }; timer.schedule(task, delay); } public static void showToast(Context context, String str) { Toast toast = Toast.makeText(context, str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.show(); } public static void showLongToast(Context context, String str) { Toast toast = Toast.makeText(context, str, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.show(); } public void cancelDiaog(boolean cancel){ if(progress!=null){ progress.cancelKey(cancel); } progress=null; str=""; }
Demo 下载地址 :图灵xmpp
第一次写博客 好紧张,欢迎各路大神指点
1 0
- 接入图灵机器人实现XMPP
- 图灵机器人接入
- 图灵机器人API接入
- 图灵机器人SDK接入指南
- 图灵机器人快速接入教程
- 网站智能客服--图灵机器人接入方式
- 个人微信接入图灵机器人(基于Python)
- 个人微信接入图灵机器人(基于Python)
- 使用nodejs接入物联网完成图灵机器人对话
- 个人微信接入图灵机器人(python版)
- Android图灵机器人的实现(一)
- 图灵机器人之Python实现
- 【毕业设计】图灵机器人的实现
- Python实现图灵机器人交互
- 微信开发学习(2)——微信平台接入图灵机器人
- 微信企业号开发之图灵机器人接入(4)
- 五分钟创建个人公众号并接入图灵机器人
- 乐视kido发布首款智能儿童手表:接入图灵机器人引擎
- 利用ShellExecuteEx手动提升用户特权,以管理员权限来运行程序。win7会有UAC弹窗
- java 1.6.0_38-b05 vm 20.13-b02 GC优化手记
- Dubbo Demo
- 1...Numpy for Python Users
- block传值
- 接入图灵机器人实现XMPP
- android关于EditText与其光标
- Linux运维基础采集项
- EditText 动态切换密码 更改为"*"
- 大型网站架构演变和知识体系
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第十二章 预处理程序
- 如何修改eclipse 中Android的预览layout布局背景颜色
- 231Power of Two
- Android学习——在线查看android源代码的3种方式