Android Development-Turing chat robot interface reference

Source: Internet
Author: User

Reprint Annotated Source: http://www.cnblogs.com/frank-zouxu/p/4121601.html

In the first few days, by chance to see the news, the Turing machine has provided the developer with the Api,api address: http://www.tuling123.com/openapi/, because this API can customize their own chat robot, which in my opinion, is a very interesting thing, So I tried to test the API in the Android app as follows (Figure 1):

Figure 1

The main code is as follows:

 Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateString Requesturl; PrivateListView LV; PrivateEditText Et_input; PrivateChatlistadapter chat_adp; PrivateContext CTX; PrivateInputmethodmanager IMM; PrivateButton et_sendmsg;@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); CTX= This; IMM=(Inputmethodmanager) Getsystemservice (Context.input_method_service);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {LV=(ListView) Findviewbyid (r.id.lv_conmunicate); Et_input=(EditText) Findviewbyid (r.id.et_input); Et_input.setonclicklistener ( This); Et_sendmsg=(Button) Findviewbyid (r.id.bt_sendmsg); Et_sendmsg.setonclicklistener ( This); CHAT_ADP=NewChatlistadapter (CTX);    Lv.setadapter (CHAT_ADP); }
@Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.bt_sendmsg:Newasyreq (). Execute (Et_input.gettext (). toString (). Trim ()); Break; } } Private classAsyreqextendsAsynctask<string, Integer, string> { PrivateString result; PrivateDialogshowstyle Dialog; @Overrideprotected voidOnPreExecute () {//TODO auto-generated Method Stub//Hide Soft Keyboard if(Imm.isactive ()) {(Inputmethodmanager) Getsystemservice (Context.input_method_service)) . Hidesoftinputfromwindow (Et_input.getwindowtoken (), inputmethodmanager.hide_not_a lways); } intNetType =NewNetworkutils (mainactivity. This). Getnettype (); Switch(netType) { Caseconfigs.nonet://handler.sendemptymessage (configs.nonet);Toast.maketext (mainactivity. This, "Please check your network connection", Toast.length_long). Show (); //canceling the execution of a task This. Cancel (true); Break; Caseconfigs.slownet://handler.sendemptymessage (configs.slownet);Dialog =NewDialogshowstyle (mainactivity. This, "Little Turing is pondering ..."); Dialog.dialogshow (); CaseConfigs.WIFI:chat_adp.add ("I:" +Et_input.gettext (). toString ()); Break; }} @Overrideprotectedstring Doinbackground (String ... params) {Try { //prevent the GET request from appearing garbled in ChineseString INFO = Urlencoder.encode (Params[0], "Utf-8"); Requesturl= Configs.api_url + Configs.apikey + "&info=" +INFO; HttpGet Request=NewHttpGet (Requesturl); HttpResponse response; Response=Newdefaulthttpclient (). Execute (Request); if(Response.getstatusline (). Getstatuscode () = = 200) {result=entityutils.tostring (Response.getentity ()); Result=jsonutils.analysisinfo (Result); } } Catch(Exception e) {e.printstacktrace (); } returnresult; } @Overrideprotected voidOnPostExecute (String result) {if(Dialog! =NULL) {Dialog.dialogdismiss (); Dialog=NULL; } chat_adp.add ("Turing:" +result); Et_input.settext (""); } }}

Android Development-Turing chat robot interface reference

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.