关于java:Android-执行-HTTP-GET-请求

5次阅读

共计 2041 个字符,预计需要花费 6 分钟才能阅读完成。

MainActivity 中只有一个 TextView 用于输入 GET 申请的后果:

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView1 = findViewById(R.id.textView1);
        AsyncGetHttp task_get = new AsyncGetHttp();
        task_get.execute("https://www.baidu.com");
    }

    class AsyncGetHttp extends AsyncTask<String, Float, String> {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        @Override
        protected void onPreExecute() {// 工作刚开始的时候执行}

        @Override
        protected String doInBackground(String... params) {
            try {URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                // 不应用缓存
                connection.setUseCaches(false);
                // 应该指的是与服务器建设连贯的最大工夫
                connection.setConnectTimeout(1000);
                // 读取数据的最大工夫,两个最大工夫不要设置得太短,不然会很容易呈现超时的异样
                connection.setReadTimeout(6000);
                // 如果不是失常的申请后果则抛出异样,留神获取后果的步骤肯定要在申请内容给完之后
                if (connection.getResponseCode() != 200) {throw new Exception("http error:" + connection.getResponseCode() + "," + connection.getResponseMessage());
                }
                // 应用 InputStream 进行接管操作
                InputStream input = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder response = new StringBuilder();
                // 这里把 reader 中的内容一行行地读取到 response 中
                String line;
                while ((line = reader.readLine()) != null) {response.append(line);
                }
                // 返回最终的后果
                return response.toString();} catch (Exception ex) {
                // 返回谬误音讯
                return ex.getMessage();} finally {
                // 申请完结后开释资源
                if (reader != null) {
                    try {reader.close();
                    } catch (Exception ignore) {}}
                if (connection != null) {connection.disconnect();
                }
            }
        }

        @Override
        protected void onProgressUpdate(Float... progresses) {// 解决进度报告}

        @Override
        protected void onPostExecute(String result) {
            // 工作实现就会到此
            textView1.setText(result);
        }

        @Override
        protected void onCancelled() {// 被动勾销工作}
    }
}

这里定义了一个继承自 AsyncTask 的外部类,应用外部类的起因是在 AsyncTaskonPostExecute 回调办法中可能间接应用 MainActivity 创立时获取的 TextView 对象。

正文完
 0