接入图灵机器人实现XMPP

来源:互联网 发布:哪里能找到工资数据 编辑:程序博客网 时间:2024/04/30 09:09

接入图灵机器人实现XMPP

转载请注明出处

  • 注册图灵帐号密码,得到key
  • 逻辑处理

1.注册图灵信息。

网站地址: [ 图灵 ]
得到appkey

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