2010.10.31———Android 04
内容一
*********************************************
从网络获取数据
*********************************************
1、web权限
2、获得指定路径下内容或文件的二进制数据
3、根据二进制数据转换为指定内容或者文件
A、从网络获取图片
B、从网络获取html源码
html源码就相当于文本了
把需要滚动的内容放到ScrollView里面
滚动条效果:
<ScrollView>
滚动显示的内容
</ScrollView>
内容一
*********************************************
从网络获取数据
*********************************************
1、web权限
<!-- 访问internet权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
2、获得指定路径下内容或文件的二进制数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置连接超时 5秒
conn.setConnectTimeout(5000);
//以get方式发起请求
conn.setRequestMethod("GET");
//
if(conn.getResponseCode() == 200){
//得到网络返回的输入流
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer)) !=-1);{
bos.write(buffer,0,len);
}
return bos.toByteArray();
}
3、根据二进制数据转换为指定内容或者文件
A、从网络获取图片
if(data!=null){
//根据图片的二进制数据,构建位图
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//将图片显示在ImageView控件上
image.setImageBitmap(bitmap);
}
B、从网络获取html源码
html源码就相当于文本了
if(data!=null){
String htmlText = new String(data,"UTF-8");
html.setText(htmlText);
}else{
Toast.makeText(MainActivity.this, R.string.htmlError, 1).show();
}
把需要滚动的内容放到ScrollView里面
滚动条效果:
<ScrollView>
滚动显示的内容
</ScrollView>