//进行流的读写
byte[] buffer = new byte[1024 * 8];
//创建一个写到内存的字节数组输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer,0,len);
byteArrayOutputStream.flush();//不断的刷新缓存
}
byteArrayOutputStream.close();//进行关闭流通道
result = byteArrayOutputStream.toString();//将写入的字节流转化为字符串
//1.创建一个Url对象
URL url = new URL(url_str);
//2.获取一个UrlConnection对象
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//3.为UrlConnection对象设置一些请求的参数,请求方式,连接的超时时间
connection.setRequestMethod("GET");//设置请求方式
connection.setConnectTimeout(1000*10);//设置超时时间
//4.在获取url请求的数据前需要判断响应码,200 :成功,206:访问部分数据成功 300:跳转或重定向 400:错误 500:服务器异常
int code = connection.getResponseCode();
if(code == 200){
//5.获取有效数据,并将获取的流数据解析成String
InputStream inputStream = connection.getInputStream();
String result = StreamUtils.streamToString(inputStream);
1.主线程中创建一个Handler
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
};
};
2.重写handler的handlermessage方法
3.子线程中创建一个Message对象,将获取的数据绑定给msg
Message msg = new Message();
//另一种方式:Message msg = Messge.obtain;
msg.obj = result;
4.主线程中的handler对象在子线程中将message发送给主线程
handler.sendMessage(msg);
5.主线程中handlermessage方法接受子线程发来的数据,就可以做更新UI的操作。
public class MainActivity extends AppCompatActivity {
private EditText et_url;
private Button btn_looksource;
private TextView tv_sourceshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1,找到控件ID
et_url = (EditText)findViewById(R.id.ev_url);
btn_looksource = (Button)findViewById(R.id.btn_looksource);
tv_sourceshow = (TextView)findViewById(R.id.source_show);
//2,设置点击事件
btn_looksource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//3,获取URL地址
final String url_str = et_url.getText().toString().trim();
//String results ="";
//3.1进行判断URL是否为空
if (TextUtils.isEmpty(url_str)) {
Toast.makeText(MainActivity.this, "url地址不能为空的", Toast.LENGTH_SHORT).show();
return;
}
System.out.println("oclick方法线程:"+Thread.currentThread().getName());
//创建子线程对象
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("oclick方法runnable线程:"+Thread.currentThread().getName());
//4,请求URL地址
//4.1创建一个URL对象
URL url = new URL(url_str);
//4.2获取URLConnection对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//4.3位URLconnection设置请求参数
connection.setRequestMethod("GET");
connection.setConnectTimeout(5 * 1000);
//4.4获取请求响应码并进行判断
int code = connection.getResponseCode();
if (code == 200) {
//4.5获取数据并且解析成字符串
InputStream inputStream = connection.getInputStream();
//4.6进行获取string数据读出来的
String results = StreamUtils.streanTosting(inputStream);
//5,将数据显示到TExtview 上
//tv_sourceshow.setText(results);
//☆☆☆3.子线中创建一个Message对象,为了携带子线程中获取的数据给主线程。
//Message msg = new Message();
//msg.obj = result;//将获取的数据封装到msg中。
//☆☆☆4.使用handler对象将message发送到主线程。
//handler.sendMessage(msg);
Message msg = new Message();
msg.obj = results;
handler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
//☆☆☆1.在主线程中创建一个Handler对象
private Handler handler = new Handler(){
//☆☆☆2.重写handler的handlermessage方法,用来接收子线程中发来的消息
public void handleMessage(android.os.Message msg) {
//☆☆☆5.接收子线程发送的数据,处理数据。
String result = (String) msg.obj;
//☆☆☆6.当前方法属于主线程可以做UI的更新
//五.获取服务器返回的内容,显示到textview上
tv_sourceshow.setText(result);
};
};
}
public class StreamUtils {
public static String streanTosting(InputStream inputStream){
String result = "";//保存字符串
try {
//进行流的读写
byte[] buffer = new byte[1024 * 8];
//创建一个写到内存的字节数组输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer,0,len);
byteArrayOutputStream.flush();//不断的刷新缓存
}
byteArrayOutputStream.close();//进行关闭流通道
result = byteArrayOutputStream.toString();//将写入的字节流转化为字符串
}catch (Exception e){
e.printStackTrace();
}
return result;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有