socket获取http数据成功一段时间后APP崩溃并报错
写了一类在activity里调用使用
public void MYimportant(){
new Thread(() -> {
Log.e("test","打开了main方法");
new Thread(runnable).start();
while (!isReceive)
stringhttp="GET /getSysTime.do HTTP/1.1\r\n" +//请求行
"Host: quan.suning.com:80\r\n" + "\r\n";//请求头
new Thread(send).start();
}).start();
}
Runnable runnable= () -> {
Log.e("test","线程runnable已运行");
try {
socket= new Socket("quan.suning.com", 80);
Log.e("text","socket已连接客户端");
} catch (IOException e) {
e.printStackTrace();
}
isReceive=true;
Receive receive=null;
try {
if(socket!=null){
receive=new Receive(socket);
Log.e("test","已把socket写入Receive类进行读取输入流");
}
} catch (IOException e) {
e.printStackTrace();
}
if(receive!=null){
receive.start();
Log.e("test","开启Receive进行读取");
System.out.println("----connected success----");
}
};
Runnable send= new Runnable() {
@Override
public void run() {
Log.e("test","线程send已运行");
try {
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
printWriter.println(stringhttp);
Log.e("test","已向服务器发送数据:"+stringhttp);
} catch (IOException e) {
e.printStackTrace();
}
}
};
private class Receive extends Thread{
private InputStream inputStream;
Receive(Socket socket) throws IOException {
Log.e("test","得到输入流");
inputStream = socket.getInputStream();
}
@Override
public void run() {
super.run();
while (isReceive){
byte[] bytes= new byte[1024];
try {
inputStream.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
String str= new String(bytes, StandardCharsets.UTF_8).trim();
Message message = Message.obtain();
message.obj=str;
handler.sendMessage(message);
}
}
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i = (msg.obj + "").indexOf("sysTime1");
time = (msg.obj + "").substring(i + 11, i + 11 + 14);
Log.e("test", "得到数据:" + msg.obj + "得到时间:" + time);
}
};
}
错误指向我创建的Handler机制那个位置