服务连接函数重构
This commit is contained in:
parent
619fd39a92
commit
9d37de4bfc
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Sat Mar 29 20:07:25 GMT 2025
|
#Sun Mar 30 06:06:05 GMT 2025
|
||||||
stageCount=2
|
stageCount=2
|
||||||
libraryProject=libaes
|
libraryProject=libaes
|
||||||
baseVersion=15.2
|
baseVersion=15.2
|
||||||
publishVersion=15.2.1
|
publishVersion=15.2.1
|
||||||
buildCount=71
|
buildCount=85
|
||||||
baseBetaVersion=15.2.2
|
baseBetaVersion=15.2.2
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Sat Mar 29 20:07:25 GMT 2025
|
#Sun Mar 30 06:06:05 GMT 2025
|
||||||
stageCount=2
|
stageCount=2
|
||||||
libraryProject=libaes
|
libraryProject=libaes
|
||||||
baseVersion=15.2
|
baseVersion=15.2
|
||||||
publishVersion=15.2.1
|
publishVersion=15.2.1
|
||||||
buildCount=71
|
buildCount=85
|
||||||
baseBetaVersion=15.2.2
|
baseBetaVersion=15.2.2
|
||||||
|
@ -156,60 +156,33 @@ public class WinBollClientService extends Service implements IWinBollClientServi
|
|||||||
class WinBollClientThread extends Thread {
|
class WinBollClientThread extends Thread {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
//ToastUtils.show("WinBollClientThread");
|
|
||||||
super.run();
|
super.run();
|
||||||
|
LogUtils.d(TAG, "run syncWinBollClientThreadStatus");
|
||||||
syncWinBollClientThreadStatus();
|
syncWinBollClientThreadStatus();
|
||||||
if (mIsEnableService) {
|
if (mIsEnableService) {
|
||||||
if (mIsWinBollClientThreadRunning == false) {
|
if (mIsWinBollClientThreadRunning == false) {
|
||||||
// 设置运行状态
|
// 设置运行状态
|
||||||
mIsWinBollClientThreadRunning = true;
|
mIsWinBollClientThreadRunning = true;
|
||||||
|
|
||||||
LogUtils.d(TAG, "WinBollClientThread run()");
|
LogUtils.d(TAG, "WinBollClientThread run()");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 唤醒守护进程
|
// 唤醒守护进程
|
||||||
//wakeupAndBindAssistant();
|
//wakeupAndBindAssistant();
|
||||||
// String username = "";
|
|
||||||
// String password = "";
|
|
||||||
// String targetUrl= "";
|
|
||||||
//
|
|
||||||
// if (GlobalApplication.isDebuging()) {
|
|
||||||
// username = PrefUtils.getString(WinBollClientService.this, "metDevUserName", "");
|
|
||||||
// password = PrefUtils.getString(WinBollClientService.this, "metDevUserPassword", "");
|
|
||||||
// } else {
|
|
||||||
// username = "WinBoll";
|
|
||||||
// password = "WinBollPowerByZhanGSKen";
|
|
||||||
// }
|
|
||||||
// targetUrl = "https://" + (GlobalApplication.isDebuging() ?"dev.winboll": "winboll") + ".cc/api/"; // 替换为实际测试的URL
|
|
||||||
//
|
|
||||||
while (mIsEnableService) {
|
while (mIsEnableService) {
|
||||||
// 显示运行状态
|
// 显示运行状态
|
||||||
//LogUtils.d(TAG, String.format("targetUrl %s", targetUrl));
|
WinBollServiceStatusView.startConnection();
|
||||||
|
|
||||||
// WinBollServerConnectionTestThread testThread = new WinBollServerConnectionTestThread(
|
|
||||||
// targetUrl,
|
|
||||||
// username,
|
|
||||||
// password,
|
|
||||||
// 15000, // 连接超时15秒
|
|
||||||
// 20000, // 读取超时20秒
|
|
||||||
// 3 // 最大重试次数
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// testThread.start();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(5 * 1000);
|
Thread.sleep(10 * 1000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
}
|
}
|
||||||
|
LogUtils.d(TAG, "while syncWinBollClientThreadStatus");
|
||||||
syncWinBollClientThreadStatus();
|
syncWinBollClientThreadStatus();
|
||||||
//ToastUtils.show("syncServiceThreadStatus OK.");
|
|
||||||
//ToastUtils.show("mIsExist : " + Boolean.toString(!mIsEnableService));
|
|
||||||
//break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 服务进程退出, 重置进程运行状态
|
// 服务进程退出, 重置进程运行状态
|
||||||
|
WinBollServiceStatusView.stopConnection();
|
||||||
mIsWinBollClientThreadRunning = false;
|
mIsWinBollClientThreadRunning = false;
|
||||||
mWinBollClientThread = null;
|
mWinBollClientThread = null;
|
||||||
}
|
}
|
||||||
|
@ -1,149 +0,0 @@
|
|||||||
package cc.winboll.studio.libaes.winboll;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author ZhanGSKen@AliYun.Com
|
|
||||||
* @Date 2025/03/29 15:57:28
|
|
||||||
* @Describe WinBoll 服务器服务情况测试访问进程。
|
|
||||||
*/
|
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
|
||||||
import java.io.IOException;
|
|
||||||
import okhttp3.Call;
|
|
||||||
import okhttp3.Callback;
|
|
||||||
import okhttp3.Credentials;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
public class WinBollServerConnectionTestThread extends Thread {
|
|
||||||
|
|
||||||
public static final String TAG = "WinBollClientService";
|
|
||||||
|
|
||||||
private final String url;
|
|
||||||
private final String username;
|
|
||||||
private final String password;
|
|
||||||
private final int connectTimeout;
|
|
||||||
private final int readTimeout;
|
|
||||||
private final int maxRetries;
|
|
||||||
private boolean testComplete = false;
|
|
||||||
|
|
||||||
public WinBollServerConnectionTestThread(String url, String username, String password) {
|
|
||||||
this(url, username, password, 10000, 10000, 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
public WinBollServerConnectionTestThread(String url, String username, String password,
|
|
||||||
int connectTimeout, int readTimeout, int maxRetries) {
|
|
||||||
this.url = url;
|
|
||||||
this.username = username;
|
|
||||||
this.password = password;
|
|
||||||
this.connectTimeout = connectTimeout;
|
|
||||||
this.readTimeout = readTimeout;
|
|
||||||
this.maxRetries = maxRetries;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
LogUtils.d(TAG, String.format("run() url %s\nusername %s\npassword %s", url, username, password));
|
|
||||||
// 构建包含认证信息的请求
|
|
||||||
String credential = Credentials.basic(username, password);
|
|
||||||
LogUtils.d(TAG, String.format("credential %s", credential));
|
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(url)
|
|
||||||
.header("Accept", "text/plain") // 设置正确的Content-Type头
|
|
||||||
.header("Authorization", credential)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Call call = client.newCall(request);
|
|
||||||
call.enqueue(new Callback() {
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call call, IOException e) {
|
|
||||||
// 处理网络请求失败
|
|
||||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResponse(Call call, Response response) throws IOException {
|
|
||||||
if (!response.isSuccessful()) {
|
|
||||||
LogUtils.d(TAG, "Unexpected code " + response, Thread.currentThread().getStackTrace());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 读取响应体作为字符串,注意这里可能需要解码
|
|
||||||
String text = response.body().string();
|
|
||||||
org.jsoup.nodes.Document doc = org.jsoup.Jsoup.parse(text);
|
|
||||||
LogUtils.d(TAG, doc.text());
|
|
||||||
|
|
||||||
// 使用id选择器找到具有特定id的元素
|
|
||||||
org.jsoup.nodes.Element elementWithId = doc.select("#LastRelease").first(); // 获取第一个匹配的元素
|
|
||||||
|
|
||||||
// 提取并打印元素的文本内容
|
|
||||||
//mszNewestAppPackageName = elementWithId.text();
|
|
||||||
//ToastUtils.delayedShow(text + "\n" + mszNewestAppPackageName, 5000);
|
|
||||||
|
|
||||||
//mHandler.sendMessage(mHandler.obtainMessage(MSG_APPUPDATE_CHECKED));
|
|
||||||
} catch (Exception e) {
|
|
||||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// int retryCount = 0;
|
|
||||||
// while (!testComplete && retryCount <= maxRetries) {
|
|
||||||
// LogUtils.d(TAG, "while (!testComplete && retryCount <= maxRetries) {");
|
|
||||||
// try {
|
|
||||||
// Response response = client.newCall(request).execute();
|
|
||||||
// if (response.isSuccessful()) {
|
|
||||||
// String responseBody = response.body().string();
|
|
||||||
// if ("OK".equalsIgnoreCase(responseBody.trim())) {
|
|
||||||
// LogUtils.d(TAG, "[" + new java.util.Date() + "] 测试成功,服务器返回OK");
|
|
||||||
// testComplete = true;
|
|
||||||
// } else {
|
|
||||||
// LogUtils.d(TAG, "[" + new java.util.Date() + "] 响应内容不符合预期:" + responseBody);
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// LogUtils.d(TAG, "[" + new java.util.Date() + "] 请求失败,状态码:" + response.code());
|
|
||||||
// }
|
|
||||||
// } catch (IOException e) {
|
|
||||||
// LogUtils.d(TAG, "[" + new java.util.Date() + "] 连接异常:" + e.getMessage());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (!testComplete) {
|
|
||||||
// try {
|
|
||||||
// Thread.sleep(2000); // 等待2秒后重试
|
|
||||||
// } catch (InterruptedException e) {
|
|
||||||
// Thread.currentThread().interrupt();
|
|
||||||
// LogUtils.d(TAG, "测试线程被中断");
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// retryCount++;
|
|
||||||
// LogUtils.d(TAG, "[" + new java.util.Date() + "] 第" + retryCount + "次重试...");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (testComplete) {
|
|
||||||
//
|
|
||||||
// } else {
|
|
||||||
// LogUtils.d(TAG, "[" + new java.util.Date() + "] 达到最大重试次数,测试失败");
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
|
||||||
// String targetUrl = "http://your-protected-server.com";
|
|
||||||
// String username = "your_username";
|
|
||||||
// String password = "your_password";
|
|
||||||
//
|
|
||||||
// WinBollServerConnectionTestThread testThread = new WinBollServerConnectionTestThread(
|
|
||||||
// targetUrl,
|
|
||||||
// username,
|
|
||||||
// password,
|
|
||||||
// 15000, // 连接超时15秒
|
|
||||||
// 20000, // 读取超时20秒
|
|
||||||
// 3 // 最大重试次数
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// testThread.start();
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,106 @@
|
|||||||
|
package cc.winboll.studio.libaes.winboll;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen@AliYun.Com
|
||||||
|
* @Date 2025/03/29 15:57:28
|
||||||
|
* @Describe WinBoll 服务器服务情况测试访问进程。
|
||||||
|
*/
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import java.io.IOException;
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
|
import okhttp3.Credentials;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
// 新增自定义回调接口
|
||||||
|
interface TextCallback {
|
||||||
|
void onSuccess(String text);
|
||||||
|
void onFailure(Exception e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WinBollServerConnectionThread extends Thread {
|
||||||
|
|
||||||
|
public static final String TAG = "WinBollClientService";
|
||||||
|
|
||||||
|
private final String url;
|
||||||
|
private final String username;
|
||||||
|
private final String password;
|
||||||
|
private final int connectTimeout;
|
||||||
|
private final int readTimeout;
|
||||||
|
private final int maxRetries;
|
||||||
|
private final TextCallback callback; // 新增回调成员变量
|
||||||
|
|
||||||
|
// 新增带回调的构造函数
|
||||||
|
public WinBollServerConnectionThread(String url, String username, String password, TextCallback callback) {
|
||||||
|
this(url, username, password, 10000, 10000, 5, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改原有构造函数,添加回调参数
|
||||||
|
public WinBollServerConnectionThread(String url, String username, String password,
|
||||||
|
int connectTimeout, int readTimeout, int maxRetries, TextCallback callback) {
|
||||||
|
this.url = url;
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.connectTimeout = connectTimeout;
|
||||||
|
this.readTimeout = readTimeout;
|
||||||
|
this.maxRetries = maxRetries;
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
LogUtils.d(TAG, String.format("run() url %s\nusername %s\npassword %s", url, username, password));
|
||||||
|
String credential = Credentials.basic(username, password);
|
||||||
|
LogUtils.d(TAG, String.format("credential %s", credential));
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.header("Accept", "text/plain")
|
||||||
|
.header("Authorization", credential)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
call.enqueue(new Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call call, IOException e) {
|
||||||
|
// 优先调用自定义回调
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onFailure(e);
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call call, Response response) throws IOException {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onFailure(new Exception("Unexpected code " + response));
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, "Unexpected code " + response, Thread.currentThread().getStackTrace());
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
String text = response.body().string();
|
||||||
|
// 优先调用自定义回调
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onSuccess(text);
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, text);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onFailure(e);
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -19,20 +19,12 @@ import android.widget.ImageView;
|
|||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import cc.winboll.studio.libaes.winboll.WinBollClientService;
|
import cc.winboll.studio.libaes.winboll.WinBollClientService;
|
||||||
|
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
import cc.winboll.studio.libapputils.R;
|
import cc.winboll.studio.libapputils.R;
|
||||||
import com.hjq.toast.ToastUtils;
|
import cc.winboll.studio.libapputils.utils.PrefUtils;
|
||||||
import java.io.IOException;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import okhttp3.Authenticator;
|
|
||||||
import okhttp3.Call;
|
|
||||||
import okhttp3.Callback;
|
|
||||||
import okhttp3.Credentials;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
import okhttp3.Route;
|
|
||||||
//import okhttp3.Authenticator;
|
//import okhttp3.Authenticator;
|
||||||
//import okhttp3.Credentials;
|
//import okhttp3.Credentials;
|
||||||
//import okhttp3.OkHttpClient;
|
//import okhttp3.OkHttpClient;
|
||||||
@ -47,9 +39,10 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
public static final int MSG_CONNECTION_INFO = 0;
|
public static final int MSG_CONNECTION_INFO = 0;
|
||||||
public static final int MSG_UPDATE_CONNECTION_STATUS = 1;
|
public static final int MSG_UPDATE_CONNECTION_STATUS = 1;
|
||||||
|
|
||||||
|
static WinBollServiceStatusView _WinBollServiceStatusView;
|
||||||
Context mContext;
|
Context mContext;
|
||||||
//boolean mIsConnected;
|
//boolean mIsConnected;
|
||||||
ConnectionThread mConnectionThread;
|
volatile ConnectionThread mConnectionThread;
|
||||||
|
|
||||||
String mszServerHost;
|
String mszServerHost;
|
||||||
WinBollClientService mWinBollService;
|
WinBollClientService mWinBollService;
|
||||||
@ -117,6 +110,8 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initView() {
|
void initView() {
|
||||||
|
_WinBollServiceStatusView = this;
|
||||||
|
|
||||||
mImageView = new ImageView(mContext);
|
mImageView = new ImageView(mContext);
|
||||||
setImageViewByConnection(mImageView, false);
|
setImageViewByConnection(mImageView, false);
|
||||||
mConnectionStatus = getConnectionStatus();
|
mConnectionStatus = getConnectionStatus();
|
||||||
@ -135,21 +130,10 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
WinBollClientServiceBean.saveBean(mContext, bean);
|
WinBollClientServiceBean.saveBean(mContext, bean);
|
||||||
Intent intent = new Intent(mContext, WinBollClientService.class);
|
Intent intent = new Intent(mContext, WinBollClientService.class);
|
||||||
mContext.stopService(intent);
|
mContext.stopService(intent);
|
||||||
stopConnectionThread();
|
//stopConnectionThread();
|
||||||
mTextView.setText("");
|
mTextView.setText("");
|
||||||
setImageViewByConnection(mImageView, false);
|
setImageViewByConnection(mImageView, false);
|
||||||
mConnectionStatus = ConnectionStatus.DISCONNECTED;
|
mConnectionStatus = ConnectionStatus.DISCONNECTED;
|
||||||
//mConnectionStatus = ConnectionStatus.DISCONNECTED;
|
|
||||||
//
|
|
||||||
/*//ToastUtils.show("CONNECTED");
|
|
||||||
setConnectionStatusView(false);
|
|
||||||
mWinBollServerHostConnectionStatusViewHandler.postMessageText("");
|
|
||||||
if (mConnectionThread != null) {
|
|
||||||
mConnectionThread.mIsExist = true;
|
|
||||||
mConnectionThread = null;
|
|
||||||
mWinBollServerHostConnectionStatus = WinBollServerHostConnectionStatus.DISCONNECTED;
|
|
||||||
ToastUtils.show("WinBoll Server Disconnected.");
|
|
||||||
}*/
|
|
||||||
} else if (mConnectionStatus == ConnectionStatus.DISCONNECTED) {
|
} else if (mConnectionStatus == ConnectionStatus.DISCONNECTED) {
|
||||||
LogUtils.d(TAG, "Click to start service.");
|
LogUtils.d(TAG, "Click to start service.");
|
||||||
WinBollClientServiceBean bean = WinBollClientServiceBean.loadWinBollClientServiceBean(mContext);
|
WinBollClientServiceBean bean = WinBollClientServiceBean.loadWinBollClientServiceBean(mContext);
|
||||||
@ -157,16 +141,8 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
WinBollClientServiceBean.saveBean(mContext, bean);
|
WinBollClientServiceBean.saveBean(mContext, bean);
|
||||||
Intent intent = new Intent(mContext, WinBollClientService.class);
|
Intent intent = new Intent(mContext, WinBollClientService.class);
|
||||||
mContext.startService(intent);
|
mContext.startService(intent);
|
||||||
startConnectionThread();
|
//startConnectionThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if (isConnected) {
|
|
||||||
mWebView.loadUrl("https://dev.winboll.cc");
|
|
||||||
} else {
|
|
||||||
mWebView.stopLoading();
|
|
||||||
}*/
|
|
||||||
//ToastUtils.show(mDevelopHostConnectionStatus);
|
|
||||||
//LogUtils.d(TAG, "mDevelopHostConnectionStatus : " + mWinBollServerHostConnectionStatus);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
setOnClickListener(mViewOnClickListener);
|
setOnClickListener(mViewOnClickListener);
|
||||||
@ -212,31 +188,97 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void requestWithBasicAuth(final WinBollServiceViewHandler textViewHandler, String targetUrl, final String username, final String password) {
|
TextCallback apiTextCallback = new TextCallback() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(String text) {
|
||||||
|
// 处理成功响应
|
||||||
|
LogUtils.d(TAG, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Exception e) {
|
||||||
|
// 处理失败情况
|
||||||
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TextCallback cipTextCallback = new TextCallback() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(String text) {
|
||||||
|
// 处理成功响应
|
||||||
|
LogUtils.d(TAG, text);
|
||||||
|
LogUtils.d(TAG, "Develop Host Connection IP is : " + text);
|
||||||
|
mConnectionStatus = ConnectionStatus.CONNECTED;
|
||||||
|
// 获取当前时间
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
// 定义时间格式
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
|
||||||
|
// 按照指定格式格式化时间并输出
|
||||||
|
String formattedDateTime = now.format(formatter);
|
||||||
|
String msg = "ClientIP<" + formattedDateTime + ">: " + text;
|
||||||
|
mWinBollServiceViewHandler.postMessageText(msg);
|
||||||
|
mWinBollServiceViewHandler.postMessageConnectionStatus(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Exception e) {
|
||||||
|
// 处理失败情况
|
||||||
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
|
// 处理网络请求失败
|
||||||
|
setImageViewByConnection(mImageView, false);
|
||||||
|
mWinBollServiceViewHandler.postMessageText(e.getMessage());
|
||||||
|
mWinBollServiceViewHandler.postMessageConnectionStatus(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public void requestAPIWithBasicAuth() {
|
||||||
|
String targetUrl = "https://" + (GlobalApplication.isDebuging() ?"dev.winboll": "winboll") + ".cc/api/"; // 替换为实际测试的URL
|
||||||
|
requestWithBasicAuth(targetUrl, apiTextCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestCIPWithBasicAuth() {
|
||||||
|
String targetUrl = mszServerHost + "/cip/?simple=true";
|
||||||
|
requestWithBasicAuth(targetUrl, cipTextCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestWithBasicAuth(String targetUrl, TextCallback callback) {
|
||||||
|
String username = "";
|
||||||
|
String password = "";
|
||||||
|
if (GlobalApplication.isDebuging()) {
|
||||||
|
username = PrefUtils.getString(mContext, "metDevUserName", "");
|
||||||
|
password = PrefUtils.getString(mContext, "metDevUserPassword", "");
|
||||||
|
} else {
|
||||||
|
username = "WinBoll";
|
||||||
|
password = "WinBollPowerByZhanGSKen";
|
||||||
|
}
|
||||||
|
LogUtils.d(TAG, String.format("Connection Start. targetUrl %s", targetUrl));
|
||||||
|
WinBollServerConnectionThread thread = new WinBollServerConnectionThread(
|
||||||
|
targetUrl,
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
cipTextCallback
|
||||||
|
);
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*void requestWithBasicAuth(final WinBollServiceViewHandler textViewHandler, String targetUrl, final String username, final String password) {
|
||||||
// 用户名和密码,替换为实际的认证信息
|
// 用户名和密码,替换为实际的认证信息
|
||||||
//String username = "your_username";
|
//String username = "your_username";
|
||||||
//String password = "your_password";
|
//String password = "your_password";
|
||||||
LogUtils.d(TAG, "requestWithBasicAuth(...)");
|
LogUtils.d(TAG, "requestWithBasicAuth(...)");
|
||||||
LogUtils.d(TAG, String.format("targetUrl %s", targetUrl));
|
LogUtils.d(TAG, String.format("targetUrl %s", targetUrl));
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
// 构建包含认证信息的请求
|
||||||
.authenticator(new Authenticator() {
|
|
||||||
@Override
|
|
||||||
public Request authenticate(Route route, Response response) throws IOException {
|
|
||||||
String credential = Credentials.basic(username, password);
|
String credential = Credentials.basic(username, password);
|
||||||
return response.request().newBuilder()
|
LogUtils.d(TAG, String.format("credential %s", credential));
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(targetUrl)
|
||||||
|
.header("Accept", "text/plain") // 设置正确的Content-Type头
|
||||||
.header("Authorization", credential)
|
.header("Authorization", credential)
|
||||||
.build();
|
.build();
|
||||||
}
|
|
||||||
})
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(targetUrl) // 替换为实际要请求的网页地址
|
|
||||||
.build();
|
|
||||||
|
|
||||||
|
|
||||||
//Response response = client.newCall(request).execute();
|
|
||||||
|
|
||||||
Call call = client.newCall(request);
|
Call call = client.newCall(request);
|
||||||
call.enqueue(new Callback() {
|
call.enqueue(new Callback() {
|
||||||
@ -305,7 +347,7 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}*/
|
||||||
|
|
||||||
class WinBollServiceViewHandler extends Handler {
|
class WinBollServiceViewHandler extends Handler {
|
||||||
WinBollServiceStatusView mDevelopHostConnectionStatusView;
|
WinBollServiceStatusView mDevelopHostConnectionStatusView;
|
||||||
@ -340,7 +382,19 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startConnectionThread() {
|
public static void startConnection() {
|
||||||
|
if (_WinBollServiceStatusView != null) {
|
||||||
|
_WinBollServiceStatusView.startConnectionThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void stopConnection() {
|
||||||
|
if (_WinBollServiceStatusView != null) {
|
||||||
|
_WinBollServiceStatusView.stopConnectionThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void startConnectionThread() {
|
||||||
if (mConnectionStatus == ConnectionStatus.DISCONNECTED) {
|
if (mConnectionStatus == ConnectionStatus.DISCONNECTED) {
|
||||||
mConnectionStatus = ConnectionStatus.START_CONNECT;
|
mConnectionStatus = ConnectionStatus.START_CONNECT;
|
||||||
LogUtils.d(TAG, "startConnectionThread");
|
LogUtils.d(TAG, "startConnectionThread");
|
||||||
@ -350,13 +404,13 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
mConnectionThread = new ConnectionThread();
|
mConnectionThread = new ConnectionThread();
|
||||||
mConnectionThread.start();
|
mConnectionThread.start();
|
||||||
} else if (mConnectionStatus == ConnectionStatus.CONNECTING) {
|
} else if (mConnectionStatus == ConnectionStatus.CONNECTING) {
|
||||||
LogUtils.d(TAG, "mConnectionStatus == ConnectionStatus.CONNECTING");
|
//LogUtils.d(TAG, "mConnectionStatus == ConnectionStatus.CONNECTING");
|
||||||
} else {
|
} else {
|
||||||
LogUtils.d(TAG, "Unknow mConnectionStatus, can not start ConnectionThread.");
|
LogUtils.d(TAG, "Unknow mConnectionStatus, can not start ConnectionThread.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopConnectionThread() {
|
void stopConnectionThread() {
|
||||||
if (mConnectionStatus == ConnectionStatus.CONNECTED) {
|
if (mConnectionStatus == ConnectionStatus.CONNECTED) {
|
||||||
LogUtils.d(TAG, "stopConnectionThread");
|
LogUtils.d(TAG, "stopConnectionThread");
|
||||||
if (mConnectionThread != null) {
|
if (mConnectionThread != null) {
|
||||||
@ -368,6 +422,8 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ConnectionThread extends Thread {
|
class ConnectionThread extends Thread {
|
||||||
|
|
||||||
public volatile boolean mIsExist;
|
public volatile boolean mIsExist;
|
||||||
@ -387,14 +443,8 @@ public class WinBollServiceStatusView extends LinearLayout {
|
|||||||
while (mIsExist == false) {
|
while (mIsExist == false) {
|
||||||
if (mConnectionStatus == ConnectionStatus.START_CONNECT) {
|
if (mConnectionStatus == ConnectionStatus.START_CONNECT) {
|
||||||
mConnectionStatus = ConnectionStatus.CONNECTING;
|
mConnectionStatus = ConnectionStatus.CONNECTING;
|
||||||
//ToastUtils.show("WinBoll Server Connection Start.");
|
//requestAPIWithBasicAuth();
|
||||||
|
requestCIPWithBasicAuth();
|
||||||
//String targetUrl = "https://" + (GlobalApplication.isDebuging() ?"dev.winboll": "winboll") + ".cc/api/"; // 替换为实际测试的URL
|
|
||||||
//String targetUrl = "https://" + mszServerHost + "/api/"; // 这里替换成你实际要访问的网址
|
|
||||||
|
|
||||||
String targetUrl = mszServerHost + "/cip/?simple=true"; // 这里替换成你实际要访问的网址
|
|
||||||
LogUtils.d(TAG, String.format("Connection Start. targetUrl %s", targetUrl));
|
|
||||||
requestWithBasicAuth(mWinBollServiceViewHandler, targetUrl, _mUserName, _mPassword);
|
|
||||||
} else if (mConnectionStatus == ConnectionStatus.CONNECTED
|
} else if (mConnectionStatus == ConnectionStatus.CONNECTED
|
||||||
|| mConnectionStatus == ConnectionStatus.DISCONNECTED) {
|
|| mConnectionStatus == ConnectionStatus.DISCONNECTED) {
|
||||||
//ToastUtils.show("mWinBollServerHostConnectionStatus " + mConnectionStatus);
|
//ToastUtils.show("mWinBollServerHostConnectionStatus " + mConnectionStatus);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user