Handlerによる非同期化


本編はhttps://blog.csdn.net/we1less/article/details/107892391前編をもとに比較する
アクセス権
 

http
android:usesCleartextTraffic="true"

レイアウト



    

    

activity代码

package com.example.handlerdemo;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HandlerTest extends AppCompatActivity {

    ProgressBar progressBar;
    EditText editText;
    //1  Handler          handlerMessage()
    private Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            //    
            if (msg.what == 1) {
                //5.        
                String result = msg.obj.toString();
                editText.setText(result);
                //      
                progressBar.setVisibility(View.INVISIBLE);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_test);
        progressBar = findViewById(R.id.pb_test_load);
        editText = findViewById(R.id.et_test_result);


    }

    //        
    public void getSubmit1(View view) {
        //1.         
        progressBar.setVisibility(View.VISIBLE);
        //       
        new Thread() {
            public void run() {
                String path = "http://192.168.56.1:8080/Web_Server/index.jsp?name=godv&age=10";

                try {
                    final String result = requestToString(path);
                    //     
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            editText.setText(result);
                            //      
                            progressBar.setVisibility(View.INVISIBLE);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    //  Handler
    public void getSubmit2(View view) {
        //1.         
        progressBar.setVisibility(View.VISIBLE);
        //       
        new Thread() {
            public void run() {
                String path = "http://192.168.56.1:8080/Web_Server/index.jsp?name=godv&age=10";

                try {
                    String result = requestToString(path);
                    //2          Message  
                    Message message = Message.obtain();
                    //2    
                    message.what = 1;
                    //3    
                    message.obj = result;
                    //4  Handler      
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     *       ,           
     *
     * @param path :http://192.168.56.1:8080/Web_Server/index.jsp
     * @return
     * @throws Exception
     */
    public String requestToString(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.connect();
        InputStream is = connection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        baos.close();
        is.close();
        String result = baos.toString();
        connection.disconnect();
        return result;
    }
}