- runOnUiThread(new Runable(){});如果当前线程是ui线程,当前线程会立即执行,如果不会,则会将消息发送到ui线程的队列当中。不管在子线程还是主线程
new Thread(
new Runnable() {
@Override
public void run() {
try { web= edt.getText().toString().trim();
URL url=new URL(web);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
Log.d("TAG", "run: 你好me");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = httpURLConnection.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
int len;
byte[] bytes=new byte[1024];
while((len=inputStream.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,len);
}
/* Message obtain = Message.obtain();
obtain.obj=new String(byteArrayOutputStream.toByteArray());
Log.d("TAG", "run: 你好");
handler.sendMessage(obtain);*/
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(new String(byteArrayOutputStream.toByteArray()));
Log.d("TAG", "run: 你在哪里");
}
});
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
).start();
```