zoukankan      html  css  js  c++  java
  • HttpURLConnection读取网页文件

    一.关键代码

    public class MainActivity extends Activity {
    
        TextView content;
        private static final  int SHOWRESPONSE = 1;
    
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case SHOWRESPONSE:
                        String response = (String)msg.obj;
                        content.setText(response);
                        break;
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            content = (TextView)findViewById(R.id.context);
        }
    
        protected void myClick(View v){
            if( v.getId() == R.id.btn ){
                sendRequest();
            }
        }
    
        private void sendRequest(){
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpURLConnection connection = null;
                    try {
                        URL url = new URL("https://www.baidu.com");
                        connection = (HttpURLConnection)url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(8000);
                        connection.setReadTimeout(8000);
                        InputStream in = connection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                        StringBuilder response = new StringBuilder();
                        String line;
                        while( (line = reader.readLine()) != null ){
                            response.append(line);
                        }
                        Message msg = handler.obtainMessage();
                        msg.what = SHOWRESPONSE;
                        msg.obj = response.toString();
                        handler.sendMessage(msg);
    
                    }catch (Exception e){
                        e.printStackTrace();
                    }finally {
                        if(connection != null){
                            connection.disconnect();
                        }
                    }
                }
            }).start();
        }
    }

    二.布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.aotian.guo.httpurlconnectiondemo.MainActivity">
    
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/btn"
            android:onClick="myClick"/>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/btn"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
    
    
            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/context"/>
        </ScrollView>
    </RelativeLayout>

    三.所需权限

    <uses-permission android:name="android.permission.INTERNET" />
  • 相关阅读:
    卸载cuda,以及N卡驱动
    ubuntu 16.04 从gcc 5.4 安装gcc 5.3.0
    Check failed: status == CUBLAS_STATUS_SUCCESS (13 vs. 0) CUBLAS_STATUS_EXECUTION_FAILED
    ubuntu16.04 caffe cuda9.1 segnet nvidia gpu安装注意的点
    ubuntu16.04安装docker
    进程管理
    Dev TextEdit 只输入数字
    dev gridcontrol添加右键菜单
    WinForm rdlc 报表自定义datatable数据源
    DevExpress GridControl使用方法总结2 属性说明
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6755912.html
Copyright © 2011-2022 走看看