android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:layout_height="wrap_content"
android:text="请输入图片的路径" />
android:id="@+id/et_path"
android:hint="请输入图片的路径"
/>
android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="viewImage" //点击事件对应在Actitvty类中的方法android:text="浏览"/> android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/iv"android:src="@drawable/ic_launcher"android:scaleType="centerInside"/> 2、在DemoActivity类中的:public class DemoActivityextends Activity {private EditText et_path;//图片地址的输入框private ImageView iv;//显示图片的组件 @Overridepublic void onCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);et_path = (EditText) this.findViewById(R.id.et_path);iv = (ImageView)this.findViewById(R.id.iv); } //点击按钮响应方法:public void viewImage(View view){Stringpath = et_path.getText().toString().trim();//路径if(TextUtils.isEmpty(path)){Toast.makeText(this, "路径不能为空",Toast.LENGTH_SHORT).show();return;}else{// 模拟浏览器 http的get请求 获取图片数据 并展现到界面上try {URL url = new URL(path); //url不合法的异常HttpURLConnection conn=(HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000); // 网络超时的异常//服务器返回回来的状态码int code =conn.getResponseCode();if(code == 200){//获取响应数据的长度String length = conn.getContentLength()+"";//响应数据的类型String type = conn.getContentType(); // 获取服务器返回过来的流的信息InputStream is = conn.getInputStream(); //将流转成bitMap对象Bitmap bitmap =BitmapFactory.decodeStream(is); //io 异常//将bitMap设置给ImageViewiv.setImageBitmap(bitmap);}else {Toast.makeText(this, "图片的不存在,或者服务器异常",Toast.LENGTH_SHORT).show();} } catch (Exception e) {e.printStackTrace();//Toast.makeText(this,"访问图片出错", Toast.LENGTH_SHORT).show();if(e instanceofMalformedURLException){Toast.makeText(this,"url不合法", Toast.LENGTH_SHORT).show();}else if(e instanceofSocketTimeoutException){Toast.makeText(this,"网络连接超时", Toast.LENGTH_SHORT).show();}else if(e instanceofIOException){Toast.makeText(this,"照片数据错误", Toast.LENGTH_SHORT).show();}else{Toast.makeText(this,"未知错误", Toast.LENGTH_SHORT).show();}3、由于连接服务器要产生流量:所以连接服务器需要权限:在AndroidManifest.xml中。 二、网络html的查看器:1、设置布局: android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"> android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="请输入网页的路径" /> android:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/et_path"android:text="http://192.168.1.247:8080/web/index.jsp"android:hint="请输入网页的路径"/> android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="viewHtml"android:text="查看"/> //时,就会出现侧拉条。android:layout_width="match_parent"android:layout_height="match_parent"> android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/tv"/> 2、业务代码:点击按钮触发的方法:public void viewHtml(Viewview) {String path =et_path.getText().toString().trim();if("".equals(path)) {Toast.makeText(this, "路径不能为空", 0).show();return;} else {try {//创建url对象URL url = new URL(path);try {HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET"); //可以不写conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if(code == 200){ InputStream is =conn.getInputStream();try {//将输入流转成字节数组.byte[] result = StreamTools.getBytes(is);tv.setText(new String(result));} catch (Exception e) {Toast.makeText(this, "io错误", 0).show();e.printStackTrace();} } } catch (IOException e){Toast.makeText(this, "协议不支持,io错误", 0).show();e.printStackTrace();} } catch (MalformedURLException e) {Toast.makeText(this,"url不合法", 0).show();e.printStackTrace();} 3、设定连接网络的权限: 三、在客户端显示视频列表:1、设置布局: 使用ListView:显示视频播放列表:①、在layout/main.xml中。定义ListView android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" >//竖直 android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/lv"/> ②、设置listView中每个项目要显示的布局:layout/item.xml中 android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#449f9f9f" //设置项目的背景色android:orientation="horizontal">//水平 android:id="@+id/iv_item_icon"android:layout_width="wrap_content"android:layout_height="48dip"android:src="@drawable/default1" /> android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical" >//竖直 android:id="@+id/tv_item_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="标题" /> android:id="@+id/tv_item_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="播放时间"android:textColor="@color/green" />//显示字体的颜色 android:id="@+id/tv_item_count"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="点播次数"android:textColor="@color/green" /> 2、业务代码.从服务器端获取的是xml文件 先将xml文件转成channel对象的集合.①、DemoActivity 中组件的显示设置public class DemoActivity extends Activity {private ListView lvprivate ChannelService service;private List<Channel>channels;private LayoutInflater inflater; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);lv = (ListView) this.findViewById(R.id.lv);//获取列表 //获取布局填充器inflater = LayoutInflater.from(this);//创建服务类的对象service = new ChannelService(this);//调用service层的方法将服务端获取的xml转成channel对象的list集合channels = service.getChannels();//为listView设置中项目之间的分隔线颜色的高度lv.setDivider(new ColorDrawable(Color.WHITE));lv.setDividerHeight(1);//高度//为ListView设置适配器lv.setAdapter(new MyAdapter()); //listView的点击项目事件lv.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterViewparent, View view,int position, long id) {//同id获取listView中项目Channel channel = (Channel) lv.getItemAtPosition(position);String title =channel.getTitle();System.out.println("播放"+ title+"提交请求给服务器,通知播放次数加1");}});}//自定义的适配器类,将浏览过的图片保存到sd卡上,下次再浏览就直接浏览sd卡上的private class MyAdapter extends BaseAdapter { @Overridepublic int getCount() { return channels.size();} @Overridepublic Object getItem(int position) {// TODO Auto-generated methodstubreturn channels.get(position);} @Overridepublic long getItemId(int position) {// TODO Auto-generated methodstubreturn position;} @Overridepublic View getView(int position, ViewconvertView, ViewGroup parent) {/** TextView tv = newTextView(DemoActivity.this);* tv.setText(channels.get(position).getTitle());*/Channel channel = channels.get(position);//通过布局填充器将layout下的item.xml转成view对象View view = inflater.inflate(R.layout.item,null);//分别获取item.xml文件中定义的组件TextView tv_count = (TextView)view.findViewById(R.id.tv_item_count);TextView tv_time= (TextView) view.findViewById(R.id.tv_item_time);TextView tv_title = (TextView) view.findViewById(R.id.tv_item_title);//为组件设置值tv_count.setText("点播次数:" + channel.getCount());tv_time.setText("播放时间:" + channel.getTime());tv_title.setText(channel.getTitle());//获取imageView对象ImageView iv = (ImageView)view.findViewById(R.id.iv_item_icon);String iconpath =channel.getIconpath();// 判断, 判断sd卡上是否有这个图片文件存在 如果有的话 使用sd卡的图片// 如果没有 才去下载这个图片String iconname =getIconName(iconpath);//将图片封装成对象File file = new File(Environment.getExternalStorageDirectory(),iconname);if (file.exists()) {//判断sd卡上是否存在图片System.out.println("使用sd卡的缓存图片");iv.setImageURI(Uri.fromFile(file));} else {//通过地址获取图片URL url = new URL(iconpath);HttpURLConnection conn =(HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);if(conn.getResponseCode()==200){InputStream is = conn.getInputStream();Bitmap bitmap = BitmapFactory.decodeStream(is); }if (bitmap != null) {//判断从服务器端获取的图片是否存在iv.setImageBitmap(bitmap);//为imageView设置图片// 需要把图片保存在sd卡里面// format 图片的格式FileOutputStreamstream;try {stream = new FileOutputStream(file);System.out.println("使用的是下载的图片");// 数据会被写到sd卡的file对象里面bitmap.compress(CompressFormat.JPEG, 100, stream);} catch (FileNotFoundException e) {e.printStackTrace();} }// 如果为空 显示默认的图片}return view; } } /*** 获取一个图片路径对应的图片名字** @param path* @return*/private String getIconName(Stringpath) {int start = path.lastIndexOf("/");return path.substring(start + 1);}}②将服务端获取的xml转成channel对象的list集合public class ChannelService {private Context context; public ChannelService(Contextcontext) {this.context = context;} public ListgetChannels() throws Exception {//请求服务端String path =context.getResources().getString(R.string.serverurl);URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000); if( conn.getResponseCode()==200){//获取的输入流InputStream is = conn.getInputStream(); //1. 得到xml的解析器XmlPullParser parser = Xml.newPullParser();//2. 初始化解析器parser.setInput(is,"utf-8"); int type =parser.getEventType();Listchannles = new ArrayList();Channel channel = null;while(type!=XmlPullParser.END_DOCUMENT){switch (type) {case XmlPullParser.START_TAG:if("channel".equals(parser.getName())){channel = new Channel();}elseif("title".equals(parser.getName())){channel.setTitle(parser.nextText());}else if("time".equals(parser.getName())){channel.setTime(parser.nextText());}elseif("count".equals(parser.getName())){ channel.setCount(Integer.parseInt(parser.nextText()));}elseif("iconpath".equals(parser.getName())){channel.setIconpath(parser.nextText());}break;case XmlPullParser.END_TAG:if("channel".equals(parser.getName())){channles.add(channel);channel = null;}break;}type = parser.next();}return channels;} return null;} }③、服务端的xml文件
android:layout_width="wrap_content"
android:onClick="viewImage" //点击事件对应在Actitvty类中的方法
android:text="浏览"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv"
android:src="@drawable/ic_launcher"
android:scaleType="centerInside"
public class DemoActivityextends Activity {
private EditText et_path;//图片地址的输入框
private ImageView iv;//显示图片的组件
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_path = (EditText) this.findViewById(R.id.et_path);
iv = (ImageView)this.findViewById(R.id.iv);
}
//点击按钮响应方法:
public void viewImage(View view){
Stringpath = et_path.getText().toString().trim();//路径
if(TextUtils.isEmpty(path)){
Toast.makeText(this, "路径不能为空",Toast.LENGTH_SHORT).show();
return;
}else{
// 模拟浏览器 http的get请求 获取图片数据 并展现到界面上
try {
URL url = new URL(path); //url不合法的异常
HttpURLConnection conn=
(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000); // 网络超时的异常
//服务器返回回来的状态码
int code =conn.getResponseCode();
if(code == 200){
//获取响应数据的长度
String length = conn.getContentLength()+"";
//响应数据的类型
String type = conn.getContentType();
// 获取服务器返回过来的流的信息
InputStream is = conn.getInputStream();
//将流转成bitMap对象
Bitmap bitmap =BitmapFactory.decodeStream(is); //io 异常
//将bitMap设置给ImageView
iv.setImageBitmap(bitmap);
}else {
Toast.makeText(this, "图片的不存在,或者服务器异常",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
//Toast.makeText(this,
"访问图片出错", Toast.LENGTH_SHORT).show();
if(e instanceofMalformedURLException){
Toast.makeText(this,
"url不合法", Toast.LENGTH_SHORT).show();
}else if(e instanceofSocketTimeoutException){
"网络连接超时", Toast.LENGTH_SHORT).show();
}else if(e instanceofIOException){
"照片数据错误", Toast.LENGTH_SHORT).show();
"未知错误", Toast.LENGTH_SHORT).show();
android:text="请输入网页的路径" />
android:text="http://192.168.1.247:8080/web/index.jsp"
android:hint="请输入网页的路径"
android:onClick="viewHtml"
android:text="查看"
//时,就会出现侧拉条。
android:layout_height="match_parent">
android:id="@+id/tv"
public void viewHtml(Viewview) {
String path =et_path.getText().toString().trim();
if("".equals(path)) {
Toast.makeText(this, "路径不能为空", 0).show();
} else {
//创建url对象
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET"); //可以不写
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
InputStream is =conn.getInputStream();
//将输入流转成字节数组.
byte[] result = StreamTools.getBytes(is);
tv.setText(new String(result));
Toast.makeText(this, "io错误", 0).show();
} catch (IOException e){
Toast.makeText(this, "协议不支持,io错误", 0).show();
} catch (MalformedURLException e) {
Toast.makeText(this,"url不合法", 0).show();
android:orientation="vertical" >//竖直
android:id="@+id/lv"
android:background="#449f9f9f" //设置项目的背景色
android:orientation="horizontal">//水平
android:id="@+id/iv_item_icon"
android:layout_height="48dip"
android:src="@drawable/default1" />
android:gravity="center_horizontal"
android:id="@+id/tv_item_title"
android:text="标题" />
android:id="@+id/tv_item_time"
android:text="播放时间"
android:textColor="@color/green" />//显示字体的颜色
android:id="@+id/tv_item_count"
android:text="点播次数"
android:textColor="@color/green" />
public class DemoActivity extends Activity {
private ListView lv
private ChannelService service;
private List<Channel>channels;
private LayoutInflater inflater;
public void onCreate(Bundle savedInstanceState) {
lv = (ListView) this.findViewById(R.id.lv);//获取列表
//获取布局填充器
inflater = LayoutInflater.from(this);
//创建服务类的对象
service = new ChannelService(this);
//调用service层的方法将服务端获取的xml转成channel对象的list集合
channels = service.getChannels();
//为listView设置中项目之间的分隔线颜色的高度
lv.setDivider(new ColorDrawable(Color.WHITE));
lv.setDividerHeight(1);//高度
//为ListView设置适配器
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterViewparent, View view,
int position, long id) {
//同id获取listView中项目
Channel channel = (Channel) lv.getItemAtPosition(position);
String title =channel.getTitle();
System.out.println("播放"+ title+"提交请求给服务器,通知播放次数加1");
});
private class MyAdapter extends BaseAdapter {
public int getCount() {
return channels.size();
public Object getItem(int position) {
// TODO Auto-generated methodstub
return channels.get(position);
public long getItemId(int position) {
return position;
public View getView(int position, ViewconvertView, ViewGroup parent) {
/*
* TextView tv = newTextView(DemoActivity.this);
* tv.setText(channels.get(position).getTitle());
*/
Channel channel = channels.get(position);
//通过布局填充器将layout下的item.xml转成view对象
View view = inflater.inflate(R.layout.item,null);
//分别获取item.xml文件中定义的组件
TextView tv_count = (TextView)view.findViewById(R.id.tv_item_count);
TextView tv_time= (TextView) view.findViewById(R.id.tv_item_time);
TextView tv_title = (TextView) view.findViewById(R.id.tv_item_title);
//为组件设置值
tv_count.setText("点播次数:" + channel.getCount());
tv_time.setText("播放时间:" + channel.getTime());
tv_title.setText(channel.getTitle());
//获取imageView对象
ImageView iv = (ImageView)view.findViewById(R.id.iv_item_icon);
String iconpath =channel.getIconpath();
// 判断, 判断sd卡上是否有这个图片文件存在 如果有的话 使用sd卡的图片
// 如果没有 才去下载这个图片
String iconname =getIconName(iconpath);
//将图片封装成对象
File file = new File(Environment.getExternalStorageDirectory(),
iconname);
if (file.exists()) {//判断sd卡上是否存在图片
System.out.println("使用sd卡的缓存图片");
iv.setImageURI(Uri.fromFile(file));
//通过地址获取图片
URL url = new URL(iconpath);
HttpURLConnection conn =
if(conn.getResponseCode()==200){
Bitmap bitmap = BitmapFactory.decodeStream(is);
if (bitmap != null) {//判断从服务器端获取的图片是否存在
iv.setImageBitmap(bitmap);//为imageView设置图片
// format 图片的格式
FileOutputStreamstream;
stream = new FileOutputStream(file);
System.out.println("使用的是下载的图片");
// 数据会被写到sd卡的file对象里面
bitmap.compress(CompressFormat.JPEG, 100, stream);
} catch (FileNotFoundException e) {
}// 如果为空 显示默认的图片
return view;
/**
* 获取一个图片路径对应的图片名字
*
* @param path
* @return
private String getIconName(Stringpath) {
int start = path.lastIndexOf("/");
return path.substring(start + 1);
public class ChannelService {
private Context context;
public ChannelService(Contextcontext) {
this.context = context;
public ListgetChannels() throws Exception {
//请求服务端
String path =context.getResources().getString(R.string.serverurl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if( conn.getResponseCode()==200){
//获取的输入流
//1. 得到xml的解析器
XmlPullParser parser = Xml.newPullParser();
//2. 初始化解析器
parser.setInput(is,"utf-8");
int type =parser.getEventType();
Listchannles = new ArrayList();
Channel channel = null;
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
if("channel".equals(parser.getName())){
channel = new Channel();
}elseif("title".equals(parser.getName())){
channel.setTitle(parser.nextText());
else if("time".equals(parser.getName())){
channel.setTime(parser.nextText());
elseif("count".equals(parser.getName())){
channel.setCount(Integer.parseInt(
parser.nextText()));
elseif("iconpath".equals(parser.getName())){
channel.setIconpath(parser.nextText());
break;
case XmlPullParser.END_TAG:
channles.add(channel);
channel = null;
type = parser.next();
return channels;
return null;
2314
http://192.168.1.247:8080/web/a.jpg
314
http://192.168.1.247:8080/web/b.jpg
安卓用久了卡顿 苹果用久
5G有真假吗?从iPhone 11
东方联盟网培训质量如何?靠谱吗?
速度与安全可兼得!改造异步布局大幅提升客
如何为我们的 App 实现锁屏小组件
苹果 iOS 16.2 / iPadOS 16.2 开发者预览版
聊聊 iOS16 中的三种新字体宽度样式
Sourcery 的 Swift Package 命令行插件
在 iOS 16 中用 SwiftUI Charts 创建一个折
英伟达确认:对华特供「低配版」A800芯片,
移动APP安全合规检测技术浅析
苹果正式放弃iTunes!如
Android中关于日期时间与
手把手教你如何实现一个简
骗子利用Google Ads从假加
勒索软件黑客破坏数据备份
安全事件响应计划的五个步
工作3年和工作7年的程序员
IT运维没做好这一点,恭喜
身为网络工程师,你能说清
架构师教你如何设计一个高