Android (Android) Chat robot realizes code sharing _android

Source: Internet
Author: User

Today, I saw an iOS written by the Turing machine people, directly to the official website (http://www.tuling123.com/openapi/) looked at the API access, too simple, just a GET request ~ So, wrote an android version of the robot, no technical content, But it is very fun ~ just last night to see their favorite Qin when the moon, hey, little Pixiu, is my robot pet! ~

This is an Android intelligent chat robot source code, the use of imitation micro-letter style design, called the Turing machine Human API, can realize intelligent chat, storytelling, tell jokes, check the weather, check the public transport and other rich functions.

First to show you the effect of the picture:

The following is a code snippet, want the source of the small partner can leave your email address below

Package com.example.android_robot_;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.text.TextUtils;
Import Android.view.View;
Import Android.view.Window;
Import Android.view.inputmethod.InputMethodManager;
Import Android.widget.EditText;
Import Android.widget.ListView;
Import Android.widget.Toast; Import Com.example.android_robot_.bean.
Chatmessage; Import Com.example.android_robot_.bean.
Chatmessage.type;
Import Com.zhy.utils.HttpUtils;
    public class Mainactivity extends activity {/** * Show message ListView * * Private ListView Mchatview;
    /** * Text field * * Private edittext mmsg;
    /** * Store Chat message/private List Mdatas = new ArrayList ();
    /** * Adapter * * Private chatmessageadapter madapter; Private Handler Mhandler = new Handler () {public VOID handlemessage (android.os.Message msg) {Chatmessage from = (chatmessage) msg.obj;
            Mdatas.add (from);
            Madapter.notifydatasetchanged ();
        Mchatview.setselection (Mdatas.size ()-);
    };
    }; <a href= "http://home.cto.com/index.php?s=/space/" target= "_blank" > @Override </a> protected void onCreate
    (Bundle savedinstancestate)
        {super.oncreate (savedinstancestate);
        Requestwindowfeature (Window.feature_no_title);
        Setcontentview (r.layout.main_chatting);
        Initview ();
        Madapter = new Chatmessageadapter (this, mdatas);
    Mchatview.setadapter (Madapter);
        private void Initview () {Mchatview = (ListView) Findviewbyid (R.id.id_chat_listview);
        Mmsg = (edittext) Findviewbyid (r.id.id_chat_msg);
    Mdatas.add (New Chatmessage (Type.input, "I am small xiu ghpd Xiu Ghpd, very glad to serve you")); public void SendMessage (view view) {final String msg = Mmsg.gettext (). ToString ();
            if (Textutils.isempty (msg)) {Toast.maketext (this, "You have not yet filled out the information ...", Toast.length_short). Show ();
        Return
        } chatmessage to = new Chatmessage (Type.output, msg);
        To.setdate (New Date ());
        Mdatas.add (to);
        Madapter.notifydatasetchanged ();
        Mchatview.setselection (Mdatas.size ()-);
        Mmsg.settext ("");
        Close the soft keyboard Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service); Gets the instance of Inputmethodmanager if (imm.isactive ()) {//If the Imm.togglesoftinput is opened (Input
            Methodmanager.show_implicit, inputmethodmanager.hide_not_always);
                Turn off the soft keyboard and open the same method by switching the on and off state of the} new Thread () {public void run () {
                Chatmessage from = null;
          try {from = httputils.sendmsg (msg);      The catch (Exception e) {from = new Chatmessage (type.input, "server hangs ...");
                } message = Message.obtain ();
                Message.obj = from;
            Mhandler.sendmessage (message);
        };
    }.start (); }
}

The above code is to implement the entire Android chat robot code, like a friend directly to use, in the use of the process found that there are problems please feel free to contact me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.