公司自己搞了一个FTP的服务器 每次down就要手动重启,比较麻烦就自己动手搞一个小app,用于检测服务器的状态 如果down就重启服务器
先整个图:
先开始做准备工作:
1、添加网络权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ftp">
<!-- 使用网络的权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- android:networkSecurityConfig="@xml/aqwq" android 9.0不支持http请求 -->
<application
android:networkSecurityConfig="@xml/aqwq"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--必须添加-->
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在res下手动创建xml文件夹 创建xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
准备工作完了
直接上代码:
package com.example.ftp;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
private Timer timer;
private Button oks;
private TextView FtpTvs;
private StringBuffer sb;
private Handler handle;
// (2) 使用handler处理接收到的消息
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
/**
* 在这里写我们需要一直重复执行的代码
* */
// 每隔2分钟检测一次服务器的状态
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
URL url;
String temp;
sb = new StringBuffer();
try {
url = new URL("");// 根据自己的服务器地址填写(我这里就不写了)
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));// 获取输入流
while ((temp = in.readLine()) != null) {
sb.append(temp);
}
// TextView
FtpTvs.setText("服务器状态:"+sb.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
}
});
Log.d("服务器状态:", sb.toString());
in.close();
} catch (MalformedURLException me) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "你输入的URL格式有问题", Toast.LENGTH_LONG).show();
}
});
me.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// (1) 使用handler发送消息
Message message = new Message();
message.what = 0;
mHandler.sendMessage(message);
}
}, 0, 120000);//每隔2分钟使用handler发送一下消息,也就是每隔一秒执行一次,一直重复执行
initView();
}
private void initView() {
oks = (Button) findViewById(R.id.oks);
FtpTvs = (TextView) findViewById(R.id.FtpTvs);
// 点击重启服务器
oks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handle = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
Toast.makeText(MainActivity.this, "执行完毕", Toast.LENGTH_LONG).show();
}
}
};
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
final String str = NetIsOnLine("");//你的服务器地址(重启服务器的服务器地址)
System.out.println(str);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
});
Message message = new Message();
message.what = 1;
handle.sendMessage(message);
}
});
thread.start();
}
});
}
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) {
return "网路服务器未开启(客户端端口异常)";
} catch (IOException e) {
return "网路服务器未开启(IO异常)";
}
if (response.getStatusLine().getStatusCode() == 200) {
return "网路服务器开启!";
}
return "其它";
}
}
MainActivity的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/FtpTvs"
android:textSize="30dp"
android:gravity="center"
android:textColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/oks"
android:text="点击重启服务器"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
</LinearLayout>
自己用的 什么命名格式的不太规范*(不纠结啊)