• [Notes] android 爬取网页


    使用HttpURLConnection来进行http连接。
    首先打开AndroidManifests.xml,加入user permission:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.check_house">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
        <uses-permission android:name="android.permission.INTERNET" />
    
    </manifest>
    

    然后参考代码:

    public class MainActivity extends AppCompatActivity {
    
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.send_request).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    send();
                }
            });
    
            textView = (TextView) findViewById(R.id.response_data);
        }
    
        private void send() {
            //开启线程,发送请求
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpURLConnection connection = null;
                    BufferedReader reader = null;
                    try {
                        URL url = new URL("http://www.163.com");
                        connection = (HttpURLConnection) url.openConnection();
                        //设置请求方法
                        connection.setRequestMethod("GET");
                        //设置连接超时时间(毫秒)
                        connection.setConnectTimeout(5000);
                        //设置读取超时时间(毫秒)
                        connection.setReadTimeout(5000);
    
                        //返回输入流
                        InputStream in = connection.getInputStream();
    
                        //读取输入流
                        reader = new BufferedReader(new InputStreamReader(in));
                        StringBuilder result = new StringBuilder();
                        String line;
                        while ((line = reader.readLine()) != null) {
                            result.append(line);
                        }
                        show(result.toString());
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (ProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (reader != null) {
                            try {
                                reader.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (connection != null) {//关闭连接
                            connection.disconnect();
                        }
                    }
                }
            }).start();
        }
    
        /**
         * 展示
         *
         * @param result
         */
        private void show(final String result) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(result);
                }
            });
        }
    }
    
  • 相关阅读:
    java里的分支语句--程序运行流程的分类(顺序结构,分支结构,循环结构)
    Java里的构造函数(构造方法)
    Java里this的作用和用法
    JAVA中的重载和重写
    从键盘接收字符类型的数据并实现剪刀石头布的规则
    使用Notepad++编码编译时报错(已解决?)
    云就是网络,云计算呢
    使用JavaMail创建邮件和发送邮件
    mysql锁机制
    java中几种常用的设计模式
  • 原文地址:https://www.cnblogs.com/immortalBlog/p/13260296.html
Copyright © 2020-2023  润新知