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