美文网首页
每日总结-第五十一天-HttpUrlConnect

每日总结-第五十一天-HttpUrlConnect

作者: SamiraG | 来源:发表于2020-06-13 23:52 被阅读0次
package com.ep.webview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    public TextView tv;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1010) {
                String code = (String)msg.obj;
                tv.setText(code);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        getsource();
    }

    private void getsource()
    {
        new Thread(new Runnable() {

            @Override
            public void run() {
                InputStream inputStream = null;
                String path = "http://www.baidu.com";
                try {
                    // 1. 把传过来的路径转成URL
                    URL url = new URL(path);
                    // 2.通过URL 建立网络连接
                    // --> url.openConnection() 返回 URLConnection
                    // ,它是一个抽象类,这里需要通过Http协议建立连接,需要它的实现类 HttpURLConnection
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    // 3. 使用GET方法访问网络
                    urlConnection.setRequestMethod("GET");
                    // 配置网络超时时间为 10秒
                    urlConnection.setConnectTimeout(10000);

                    //4. 获取Http协议 响应码
                    int responseCode = urlConnection.getResponseCode();
                    // Http 协议 规定 响应码为200时 请求成功
                    if (responseCode == 200) {
                        // 5. 得知 请求资源成功后,获取图片文件输入流
                        inputStream = urlConnection.getInputStream();

                        // 6. 把获取到的 文件输入流 通过 系统Api 转换为 ImageView 可以识别的 Bitmap 对象

                        String code = convertStreamToString(inputStream);

                        // 7.要知道 这里现在是子线程 更新UI的操作需要再主线程(UI线程)完成。
                        Message message = mHandler.obtainMessage();
                        message.obj = code;
                        message.what = 1010;//
                        mHandler.sendMessage(message);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        try {
                            //关闭流
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

    public String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

TODO:

  • soot使用与获取函数参数
  • notes完善

相关文章

网友评论

      本文标题:每日总结-第五十一天-HttpUrlConnect

      本文链接:https://www.haomeiwen.com/subject/nfutxktx.html