android - 我想获取网页的信息为什么无法显示?

 米米丫头2502860283 发布于 2022-10-29 13:49

package com.example.myroom.network;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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 Activity implements View.OnClickListener {

public  static final int SHOW_RESPONSE=0;
private Button sendRequest;
private TextView responseText;
StringBuilder response=new StringBuilder();
private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case SHOW_RESPONSE:
                String response=(String)msg.obj;
                responseText.setText(response);
        }
        super.handleMessage(msg);
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sendRequest=(Button)this.findViewById(R.id.send_request);
    responseText=(TextView)this.findViewById(R.id.response);
    sendRequest.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if(view.getId()==R.id.send_request){
        sendRequestWithHttpURLConnection();
        responseText.setText(response);
    }
}

private void sendRequestWithHttpURLConnection() {
    new Thread(){
        @Override
        public void run() {
            HttpURLConnection connection=null;
            try{

                URL url=new URL("http://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));

                String line;
                while((line=reader.readLine())!=null){
                    response.append(line);
                }
                Message message=new Message();
                message.what=SHOW_RESPONSE;
                message.obj=response.toString();
                handler.sendMessage(message);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(connection!=null){
                    connection.disconnect();
                }
            }
            super.run();
        }
    }.start();
}

}


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

有加权限,但不显示

3 个回答
  • 照你的代码,我试了下,可以的啊

    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    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 implements View.OnClickListener {
    
        public static final int SHOW_RESPONSE = 0;
        private Button sendRequest;
        private TextView responseText;
        StringBuilder response = new StringBuilder();
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case SHOW_RESPONSE:
                        String response = (String) msg.obj;
                        responseText.setText(response);
                }
                super.handleMessage(msg);
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            sendRequest = (Button) findViewById(R.id.send_request);
            sendRequest.setOnClickListener(this);
            responseText = (TextView) findViewById(R.id.response);
        }
    
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.send_request) {
                sendRequestWithHttpURLConnection();
                responseText.setText(response);
            }
        }
    
        private void sendRequestWithHttpURLConnection() {
            new Thread() {
                @Override
                public void run() {
                    HttpURLConnection connection = null;
                    try {
    
                        URL url = new URL("https://m.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));
    
                        String line;
                        while ((line = reader.readLine()) != null) {
                            response.append(line);
                        }
                        Message message = new Message();
                        message.what = SHOW_RESPONSE;
                        message.obj = response.toString();
                        handler.sendMessage(message);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (connection != null) {
                            connection.disconnect();
                        }
                    }
                    super.run();
                }
            }.start();
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/send_request"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send request"/>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical">
    
            <TextView
                android:id="@+id/response"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </ScrollView>
    
    </LinearLayout>
    
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    2022-10-30 14:46 回答
  • 再检查一下网络权限吧

    难道你没点按钮,或者模拟器没联网

    2022-10-30 14:50 回答
  • 照第一行代码抄写的?

    2022-10-30 14:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有