Android判斷伺服器是否開啟

來源:互聯網
上載者:User
Android判斷伺服器是否開啟Android判斷伺服器是否開啟的簡單實現,代碼如下:一:java代碼:
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.params.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 savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);etAddress=(EditText) findViewById(R.id.editText1);}@Overridepublic boolean onCreateOptionsMenu(Menu 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 view){address=etAddress.getText().toString();handle=new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubif(msg.what==1){System.out.println("執行完畢!");}}};Thread thread=new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubString str=NetIsOnLine(address);System.out.println(str);Message message=new Message();message.what=1;handle.sendMessage(message);}});thread.start();}/** * 判斷網路伺服器是否開啟 * @param path  網路伺服器位址 * @return * 網路伺服器開啟 * 網路伺服器未開啟 */public  String  NetIsOnLine(String path){HttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams, 10000);HttpConnectionParams.setSoTimeout(httpParams, 10000);HttpClient client = new DefaultHttpClient(httpParams);HttpGet get = new HttpGet(path);HttpResponse response = null;try {    response = client.execute(get);} catch (ClientProtocolException e) {// TODO Auto-generated catch blockreturn "網路伺服器未開啟(用戶端連接埠異常)";} catch (IOException e) {// TODO Auto-generated catch blockreturn "網路伺服器未開啟(IO異常)";}if(response.getStatusLine().getStatusCode()==200){return "網路伺服器開啟!";}return "其它";}}

二:布局檔案:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Net</string>    <string name="action_settings">Settings</string>    <string name="hello_world">判斷伺服器是否開啟</string>    <string name="service_address">伺服器位址</string>    <string name="bt_lianjie">串連</string></resources>

三:資訊清單檔

<?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" />    <!-- 使用網路的許可權 -->    <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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.