android获取网页源码,android HttpGet获取网页源码

该博客展示了如何在Android应用中实现HTTP GET请求,从输入的网址提取数据,并在TextView中显示。代码包括布局文件的设定,EditText用于输入网址,Button触发请求,TextView展示响应内容。主活动中,按钮点击事件启动新线程进行网络请求,主线程更新UI显示响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、布局文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/url"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="输入网址"

/>

android:id="@+id/send"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="开始提取"

/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/text"

android:layout_width="match_parent"

android:layout_height="match_parent" />

2、主要代码

package com.get.demo;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.net.HttpURLConnection;

import java.net.URL;

public class MainActivity extends Activity {

EditText url;

Button send;

TextView text;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

url = (EditText) findViewById(R.id.url);

send = (Button) findViewById(R.id.send);

text = (TextView) findViewById(R.id.text);

//按钮点击事件

send.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

//执行httpGet方法

httpGet();

}

});

}

private void httpGet() {

//开子线程网络请求

new Thread(new Runnable() {

@Override

public void run() {

HttpURLConnection connection=null;

BufferedReader reader=null;

String urls=url.getText().toString();

try {

URL url=new URL(urls);

connection=(HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

connection.setReadTimeout(5000);

connection.setConnectTimeout(5000);

InputStream in=connection.getInputStream();

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

StringBuilder response =new StringBuilder();

String line;

while ((line=reader.readLine())!=null){

response.append(line);

}

showResponse(response.toString());

} catch (IOException e) {

e.printStackTrace();

}finally {

if (reader!=null){

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (connection!=null){

connection.disconnect();

}

}

}

}).start();

}

//切换为主线程

private void showResponse(final String response){

runOnUiThread(new Runnable() {

@Override

public void run() {

try {

text.setText(new String(response.getBytes(),"UTF-8"));

//把获取的网页数据赋值给变量response,并设置给TextView控件

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

});

}

}

3、效果图

3ace96925676?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值