if (plus.os.name == "Android") {
ServerSocket = plus.android.importClass("java.net.ServerSocket");
PrintWriter = plus.android.importClass("java.io.PrintWriter");
BufferedWriter = plus.android.importClass("java.io.BufferedWriter");
OutputStreamWriter = plus.android.importClass("java.io.OutputStreamWriter");
BufferedReader = plus.android.importClass("java.io.BufferedReader");
InputStreamReader = plus.android.importClass("java.io.InputStreamReader");
//var socket = new Socket("127.0.0.1", 8081);
//测试改良
var StrictMode = plus.android.importClass("android.os.StrictMode");
var Build = plus.android.importClass("android.os.Build");
if (Build.VERSION.SDK_INT > 9) {
var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
var serverSocket = new ServerSocket(12345);
socket = plus.android.importClass("java.net.Socket");
socket = serverSocket.accept();
//获取输出流
var outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
var bufferWriter = new BufferedWriter(outputStreamWriter);
out = new PrintWriter(bufferWriter, true);
out.println("88888Conneted...");
//获取输入流
var inputStreamReader = new InputStreamReader(socket.getInputStream());
var br = new BufferedReader(inputStreamReader);
//console.log(msg);
var responseLine = "";
while ((responseLine = br.readLine()) != null) { //用readLine接收数据是,会自动抛弃换行符,如果为了保持数据的格式,需要在这里加上一个换行标识符
//returnStr += responseLine + "\n";
console.log(responseLine);
}
参考网址如下:https://ask.dcloud.net.cn/question/60
https://blog.csdn.net/baidu_36583119/article/details/79697063
1 个回复
立扬 (作者)
没有遇到么?问题原因应该是br.readLine()是阻塞式接收。暂且用window.setInterval("getMsg()", 1000),代替了while 增加一行判断if (br.ready()) { msg = br.readLine();} 。问题解决,但这个貌似是个凑合的解决方式,求完美解决办法...