网络多线程

image.png

AndroidManifest.xml

image.png

activity_main.xml

image.png

MainActivity.java

  1. package com.example.nethanderdemo;
  2. import android.os.Bundle;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.TextView;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.HttpURLConnection;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import androidx.appcompat.app.AppCompatActivity;
  15. public class MainActivity extends AppCompatActivity {
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. }
  21. public Handler mHander = new Handler(){
  22. @Override
  23. public void handleMessage(Message msg) {
  24. super.handleMessage(msg);
  25. if(msg.what == 0x111){
  26. TextView textView = findViewById(R.id.tv1);
  27. textView.setText((String) msg.obj);
  28. }
  29. }
  30. };
  31. public void btnClick1(View view) {
  32. // 使用网络API 访问百度,将返回的源代码输出到TextView中
  33. // 一些常见问题
  34. // 0. 网络操作需要网络权限
  35. // 1. android中的网络操作属于耗时操作,耗时操作不允许在主线程执行
  36. // 2. 界面操作只能在主线程操作
  37. Runnable runnable = new Runnable() {
  38. @Override
  39. public void run() {
  40. String path = "http://www.baidu.com";
  41. try {
  42. // 1. 创建URL对象,连接对象,设置信息
  43. URL url = new URL(path);
  44. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  45. connection.setConnectTimeout(5000);
  46. // 2. 发出请求,获取响应结果
  47. int code = connection.getResponseCode();
  48. if(code == 200){
  49. // 读取数据,保存数据
  50. InputStream inputStream = connection.getInputStream();
  51. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  52. byte bytes[] = new byte[1024];
  53. int retCount = -1;
  54. while(true){
  55. retCount = inputStream.read(bytes,0,1024);
  56. if(retCount == -1) break;
  57. outputStream.write(bytes,0,retCount);
  58. }
  59. String string = new String(outputStream.toString());
  60. Log.d("15pb",string);
  61. // TextView textView = findViewById(R.id.tv1);
  62. // textView.setText(string);
  63. // 1. 创建消息对象
  64. Message message = new Message();
  65. message.obj = string;
  66. message.what = 0x111;
  67. // 2. 使用handler发送消息到主线程中
  68. mHander.sendMessage(message);
  69. }
  70. } catch (MalformedURLException e) {
  71. e.printStackTrace();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. };
  77. Thread thread1 = new Thread(runnable);
  78. thread1.start();
  79. }
  80. public void btnClick0(View view) {
  81. // 使用Thread,创建对象指定回调,启动线程
  82. Thread thread = new Thread(){
  83. @Override
  84. public void run() {
  85. super.run();
  86. Log.d("15pb","thread");
  87. }
  88. };
  89. thread.start();
  90. //使用Runnable创建回调对象,使用Thread启动
  91. Runnable runnable = new Runnable() {
  92. @Override
  93. public void run() {
  94. Log.d("15pb","runnable");
  95. }
  96. };
  97. Thread thread1 = new Thread(runnable);
  98. thread1.start();
  99. }
  100. }