zoukankan
html css js c++ java
安卓中使用HttpURLConnection连接网络简单示例 --Android网络编程
MainActivity.java:
package thonlon.example.cn.httpurlconnectionpro;
import android.os.Bundle;
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 {
private Button btn_req;
private TextView tv_res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_req = (Button) findViewById(R.id.btn_req);
tv_res = (TextView) findViewById(R.id.tv_res);
}
public void onClick(View view) {
sendRequestHttpURLConnection();
}
public void sendRequestHttpURLConnection() {
new Thread(new Runnable() {
@Override
public void run() {
String urlStr = "https://www.baidu.com";
HttpURLConnection conn = null;
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line);
}
showResponse(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}
}
}
}).start();
}
private void showResponse(final String response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tv_res.setText(response);
}
});
}
}
查看全文
相关阅读:
python bottle学习(二)加载配置文件
python bottle学习(一)快速入门
python 文件读写模式r,r+,w,w+,a,a+的区别(附代码示例)
Centos 虚拟机网络问题,网卡起不来,重启network服务失败
机器学习框架MXnet安装步骤
linux机器之间配置ssh无密访问
好多年没写点技术相关的东西了,今天回来看看,找找当年做程序员的感觉
vue路由传参的三种基本方式
JavaScript判断对象是否包含某个属性的几种方法
CSS实现三栏布局(左边固定、右边固定、中间自适应)的五种方式
原文地址:https://www.cnblogs.com/qikeyishu/p/9172595.html
最新文章
前端工具集整理
reset.min.css
HTTP网络相关协议知识
前端性能优化方案
react生命周期
简单实现react中虚拟DOM渲染
push to origin/master was rejected
Docker Desktop: Error response from daemon: driver failed programming external connectivity on endpoint xxx 问题
Docker zookeeper 集群 for Docker desktop (win)
Docker Desktop for Windows Experience
热门文章
centos7 断电导致 generating /run/initramfs/rdsosreport.txt 问题
Memcahced 缓存过期时间问题
dubbo rest服务(消费者) java.lang.ClassNotFoundException: org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine 错误问题
CentOS 7 端口白名单设置
修改 MySQL 自增ID的起始值
CentOS 6 端口白名单设置
我观察了99个餐厅打饭大妈和卖瓜子的老太太,得到的一些启示
从程序员到公务员——他的逃离北上广之路
python bottle学习(四)request.quest/query_string/params/body等方法介绍
python bottle学习(三)动态路由配置(通配符)
Copyright © 2011-2022 走看看