安卓开发获取网页源码,使用的是 HttpURLConnection
类
进行网络处理或者耗时操作不能在主线程进行,需要开子线程
同理子线程也不能操作主线程,所以进行 UI 更新数据需要用到 Handler
代码
new Thread(new Runnable() {
@Override
public void run() {
try {
java.net.URL urlInfo = new URL("https://xlj0.com");
// 开启请求
HttpURLConnection httpConnect = (HttpURLConnection) urlInfo.openConnection();
// 设置加载方式
httpConnect.setRequestMethod("POST");
// 设置是否从 HttpURLConnection 读入
httpConnect.setDoInput(true);
// 设置是否从 HttpURLConnection 读出
httpConnect.setDoOutput(true);
// 设置连接超时时间 - 单位: ms
httpConnect.setConnectTimeout(2 * 2500);
// 设置读取超时时间 - 单位: ms
httpConnect.setReadTimeout(2 * 2500);
// 开启连接
httpConnect.connect();
// 执行结果状态码
int httpCode = httpConnect.getResponseCode();
Log.i("Info", "状态:" + httpCode);
// 得到结果
if (httpConnect.getResponseCode() == 200) {
Log.i("Info", "获取成功 内容长度:" + httpConnect.getContentLength());
// 创建字节流组
InputStream inputStream = httpConnect.getInputStream();
// 创建字节流组输出流
ByteArrayOutputStream ziJieLiuZuShuChuLiu = new ByteArrayOutputStream();
// 创建缓存
byte[] buffer = new byte[2048];
// 创建行数
int outLength = -1;
// 循环读出
while ((outLength = inputStream.read(buffer)) != -1) {
// 读出数据
ziJieLiuZuShuChuLiu.write(buffer, 0, outLength);
}
// 字节流数据转化成字符串 - 数据获取完成
String shuJu = ziJieLiuZuShuChuLiu.toString();
// 打印数据
Log.i("inputStream", "内容:" + shuJu);
httpConnect.disconnect();
} else {
Log.i("Info", String.valueOf(httpConnect.getContentLength()));
// 发送数据
Message message = new Message();
message.what = ERROR;
hander.sendMessage(message);
httpConnect.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
用Handler
来执行主线程操作
java
// 逻辑变量
private static final int SUCCESS = 0;
private static final int ERROR = 1;
// 创建 Handler 线程
private Handler hander = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SUCCESS:
Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_LONG).show();
break;
case ERROR:
Toast.makeText(MainActivity.this, "下载出错", Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 点击事件
dianJiShiJian();
}
private void dianJiShiJian() {
// 绑定控件
final Button xiaZaiWenJian_Button = (Button) findViewById(R.id.xiaZaiWenJian_Button);
// 下载文件
xiaZaiWenJian_Button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击成功", Toast.LENGTH_LONG).show();
XiaZaiWenJian();
}
});
}
// 开启线程 XiaZaiWenJian - 开线程下载文件
private void XiaZaiWenJian () {
new Thread(new Runnable() {
@Override
public void run() {
try {
java.net.URL urlInfo = new URL("https://xlj0.com");
// 开启请求
HttpURLConnection httpConnect = (HttpURLConnection) urlInfo.openConnection();
// 设置加载方式
httpConnect.setRequestMethod("POST");
// 设置是否从 HttpURLConnection 读入
httpConnect.setDoInput(true);
// 设置是否从 HttpURLConnection 读出
httpConnect.setDoOutput(true);
// 设置连接超时时间 - 单位: ms
httpConnect.setConnectTimeout(2 * 2500);
// 设置读取超时时间 - 单位: ms
httpConnect.setReadTimeout(2 * 2500);
// 开启连接
httpConnect.connect();
// 执行结果状态码
int httpCode = httpConnect.getResponseCode();
Log.i("Info", "状态:" + httpCode);
// 得到结果
if (httpConnect.getResponseCode() == 200) {
Log.i("Info", "获取成功 内容长度:" + httpConnect.getContentLength());
// 创建字节流组
InputStream inputStream = httpConnect.getInputStream();
// 创建字节流组输出流
ByteArrayOutputStream ziJieLiuZuShuChuLiu = new ByteArrayOutputStream();
// 创建缓存
byte[] buffer = new byte[2048];
// 创建行数
int outLength = -1;
// 循环读出
while ((outLength = inputStream.read(buffer)) != -1) {
// 读出数据
ziJieLiuZuShuChuLiu.write(buffer, 0, outLength);
}
// 字节流数据转化成字符串
String shuJu = ziJieLiuZuShuChuLiu.toString();
// 更新UI线程
Message message = new Message();
message.what = SUCCESS;
hander.sendMessage(message);
// 打印数据
Log.i("inputStream", "内容:" + shuJu);
httpConnect.disconnect();
} else {
Log.i("Info", String.valueOf(httpConnect.getContentLength()));
// 发送数据
Message message = new Message();
message.what = ERROR;
hander.sendMessage(message);
httpConnect.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xiaZaiWenJian_Button"
android:text="下载文件"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。