Android开发HttpURLConnection网络请求
发布日期:2021-06-30 11:20:22
浏览次数:2
分类:技术文章
本文共 3811 字,大约阅读时间需要 12 分钟。
activity_main
MainActivity
package com.etime.myproject;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.TextView;import androidx.annotation.NonNull;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;/** * 网络请求需添加权限 **/public class MainActivity extends Activity { private TextView mResponseTextView; private Button mSendRequestButton; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ //创建Handler mHandler=new MyHandler(); mResponseTextView = (TextView) findViewById(R.id.responseTextView); mSendRequestButton = (Button) findViewById(R.id.sendRequestButton); mSendRequestButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MyThread myThread=new MyThread(); myThread.start(); } }); } private class MyHandler extends Handler{ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); //处理消息 int what=msg.what; if(what==1){ String message=(String)msg.obj; //更新UI mResponseTextView.setText(message); } } } private class MyThread extends Thread{ @Override public void run() { super.run(); HttpURLConnection connection = null; InputStream is=null; ByteArrayOutputStream bos=null; try { //创建URL URL url = new URL("https://www.baidu.com/"); //打开链接 connection = (HttpURLConnection) url.openConnection(); //设置请求方式 connection.setRequestMethod("GET"); //建立链接 connection.connect(); //获取响应码 int responseCode=connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { //读取数据 is = connection.getInputStream(); bos = new ByteArrayOutputStream(); int len = 0; byte buffer[] = new byte[1024*2]; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } //处理数据 String response = new String(bos.toByteArray()); //创建消息 Message message=new Message(); message.what=1; message.obj=response; //发送消息 mHandler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } finally { //释放资源 if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect(); } } } }}
AndroidManifest.xml
转载地址:https://it9527.blog.csdn.net/article/details/8195243 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月19日 00时59分34秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【git】使用ssh密钥访问Github
2019-04-30
python读取excel
2019-04-30
PDFBox学习总结
2019-04-30
TestNG入门程序
2019-04-30
yum源配置
2019-04-30
tar命令解压文件后造成目录权限更改
2019-04-30
检测到会话cookie中缺少HttpOnly属性
2019-04-30
Centos下Yum安装PHP5.5,5.6
2019-04-30
zabbix安装搭建php环境yum语句
2019-04-30
Linux下配置APACHE支持PHP环境
2019-04-30
Ubuntu忘记超级用户root密码,重新设置密码
2019-04-30
Ubuntu下使用Vi是方向键变乱码 退格键不能使用的解决方法
2019-04-30
redis的安装、启动、停止
2019-04-30
Linux下防火墙开启相关端口及查看已开启端口
2019-04-30
一次zabbix连接不上mysql数据库的经历
2019-04-30
Zabbix error - Invalid first parameter.
2019-04-30
通过maven打包依赖jar,打包主类,在pom.xml中设置
2019-04-30
redis中pipeline的使用
2019-04-30
linux命令seq产生序列
2019-04-30