1.解析网站并显示
因为要处理网页读取,需要开启线程,并在UI上更新,则要使用handler
注意Handler导入的是os包
下面是MainActivity
private WebView webView;
private WebView webView;
private Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_url_connection);
webView=(WebView)findViewById(R.id.webview);
new HttpThread("http://www.baidu.com",handler,webView).start();
}
子线程类
public class HttpThread extends Thread {
private String url=null;
private Handler handler;
private WebView webView;
StringBuffer sb;
public HttpThread(String url,Handler handler,WebView webView){
this.url=url;
this.handler=handler;
this.webView=webView;
}
@Override
public void run() {
try {
//获取URL对象
URL httpUrl=new URL(url);
//通过URL对象获取连接
HttpURLConnection conn= (HttpURLConnection) httpUrl.openConnection();
//设置 连接读取时长
conn.setReadTimeout(5000);
//设置以get形式请求
conn.setRequestMethod("GET");
//字符串流
sb=new StringBuffer();
//获得连接的字节输入流
InputStream is=conn.getInputStream();
//将字节输入流转换为字符输入流
InputStreamReader isr=new InputStreamReader(is);
//加载到缓存流中,只有缓存流可以直接读一行
BufferedReader reader=new BufferedReader(isr);
String str;
while ((str=reader.readLine())!=null){
sb.append(str);
}
//使用post(),在runable中更新UI
handler.post(new Runnable() {
@Override
public void run() {
webView.loadData(sb.toString(),"text/html;charset=utf-8",null);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.解析图片网址,先保存在外部存储中,在通过bitmap获取到UI中
在MainActivity中开启线程
下面的网址一定要是一个图片网址,即后缀以,jpg或其他图片格式显示
new HttpThread("http://pic.sc.chinaz.com/files/pic/pic9/201512/apic17727.jpg",handler,imageView).start();
子线程类
public class HttpThread extends Thread {
private String url=null;
private Handler handler;
private ImageView imageView;
public HttpThread(String url,Handler handler,ImageView imageView){
this.url=url;
this.handler=handler;
this.imageView=imageView;
}
@Override
public void run() {
try {
URL httpUrl=new URL(url);
//通过URL对象获取连接
HttpURLConnection conn= (HttpURLConnection) httpUrl.openConnection();
//设置 连接读取时长
conn.setReadTimeout(5000);
//设置以get形式请求
conn.setRequestMethod("GET");
//文档上只是写着设置连接允许输入,不知道是什么意思
conn.setDoInput(true);
InputStream in=conn.getInputStream();
FileOutputStream out=null;
File downloadFile=null;
Log.i("info","进入到HttpThread中");
String fileName=String.valueOf(System.currentTimeMillis());
//只有在SD卡状态为MEDIA_MOUNTED时,/mmt/sdcard目录才是可读可写的,并且可以创建目录及文件
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//获取外部存储路径,外部存储不一定是sd卡,还包括手机自带的内存,内部存储是指data/data/包名下的
File parent=Environment.getExternalStorageDirectory();
//在获取的目录下新建文件名为fileName的文件
downloadFile=new File(parent,fileName);
//文件输出流
out=new FileOutputStream(downloadFile);
}
byte[] b=new byte[2*1024];
int len;
if(out!=null){
//通过字节流的读取方法,以数组为缓存
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
}
//通过指定sd卡图片文件路径来获取bitmap
final Bitmap bitmap= BitmapFactory.decodeFile(downloadFile.getAbsolutePath());
handler.post(new Runnable() {
@Override
public void run() {
Log.i("info","写入成功");
imageView.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面获取网页图片是先存到外部存储中,然后在将图片从外部存储中取出来,当然可以直接通过网页来获取,下面的代码只是将获取的方式从文件改为了流
URL httpUrl=new URL(url);
HttpURLConnection connection=(HttpURLConnection)httpUrl.openConnection();
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
InputStream in=connection.getInputStream();
//通过获取网上的输入流,来将图片传给Bitmap
final Bitmap bitmap= BitmapFactory.decodeStream(in);
//同样通过post更新UI
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
版权声明:本文为molu_chase原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。