Android determines whether the server enables android to determine whether the server is enabled. The Code is as follows: 1. Java code:
Package com.net. main; import Java. io. ioexception; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. client. clientprotocolexception; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. methods. httpget; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. params. basichttpparams; import Org. apache. HTTP. params. httpconnectionparams; import Org. apache. HTTP. par AMS. httpparams; import android. annotation. suppresslint; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. menu; import android. view. view; import android. widget. edittext; public class mainactivity extends activity {private edittext etaddress; private handler handle; private string address; @ overrideprotected void oncreate (bundle Sav Edinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); etaddress = (edittext) findviewbyid (R. id. edittext1) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}@ suppresslint ("handlerleak") Public void lianjie (view) {address = etad Dress. gettext (). tostring (); handle = new handler () {@ overridepublic void handlemessage (Message MSG) {// todo auto-generated method stubif (MSG. what = 1) {system. out. println ("execution completed! ") ;}}; Thread = new thread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubstring STR = netisonline (Address ); system. out. println (STR); message = new message (); message. what = 1; handle. sendmessage (Message) ;}}); thread. start ();} /*** determine whether the network server is enabled * @ Param path: the network server address * @ return * The network server is enabled * The network server is not enabled */Public String netisonline (string path) {httpparams = new ba Sichttpparams (); httpconnectionparams. setconnectiontimeout (httpparams, 10000); httpconnectionparams. setsotimeout (httpparams, 10000); httpclient client = new defaulthttpclient (httpparams); httpget get = new httpget (PATH); httpresponse response = NULL; try {response = client.exe cute (get);} catch (clientprotocolexception e) {// todo auto-generated catch blockreturn "the network server is not enabled (the client port is abnormal )";} catch (ioexception E) {// todo auto-generated catch blockreturn "the network server is not enabled (IO exception)";} If (response. getstatusline (). getstatuscode () = 200) {return "the network server is enabled! ";}Return" other ";}}
Ii. layout file:
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> net </string> <string name = "action_settings"> Settings </string> <string name = "hello_world"> determines whether the server is enable </string> <string name = "service_address"> server address </string> <string name = "bt_lianjie"> connection </string> </resources>
Iii. inventory file
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com.net. main "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "Android: targetsdkversion =" 17 "/> <! -- Use Network permissions --> <uses-Permission Android: Name = "android. permission. internet "/> <application Android: allowbackup =" true "Android: icon =" @ drawable/ic_launcher "Android: Label =" @ string/app_name "Android: theme = "@ style/apptheme"> <activity Android: Name = "com.net. main. mainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> </manifest>