android 网络编程--URL获取数据/图片
首先,开始最简单的网络编程实战,URL实现网络连接,不懂的童鞋可以参考JAVA中的URL编程,其原理是一样的,在这里不再多做解释。
直接贴出实现源代码:
public class DataActivity extends Activity { private EditText imagePathText; private static final String TAG="DataActivity"; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imagePathText = (EditText) findViewById(R.id.imagepath); imageView=(ImageView)findViewById(R.id.imageView); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String path = imagePathText.getText().toString(); try { byte[] data=NetTool.getImage(path); Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length); imageView.setImageBitmap(bm); } catch (Exception e) { Log.i(TAG, e.toString()); Toast.makeText(DataActivity.this, "获得图片失败", 1).show(); } } }); // 获取网页源代码 Button sinaButton = (Button) findViewById(R.id.sinaButton); sinaButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(DataActivity.this,SinaActivity.class); startActivity(intent); } }); }
}
public class SinaActivity extends Activity { private TextView textView; private static final String TAG="SinaActivity";
// 获取网页源代码 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sina); textView=(TextView)this.findViewById(R.id.sina); try { String html=NetTool.getHtml("http://www.sina.com.cn","GBK"); textView.setText(html); } catch (Exception e) { Log.i(TAG, e.toString()); Toast.makeText(SinaActivity.this, "获得网页失败", 1).show(); } }
}
public class NetTool { /** * 获得url代码数据 * */ public static String getHtml(String path,String encoding) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(6 * 1000); // 别超过6秒。 System.out.println(conn.getResponseCode()); if(conn.getResponseCode()==200){ InputStream inputStream=conn.getInputStream(); byte[] data=readStream(inputStream); return new String(data,encoding); } return null; } /** * 获取指定路径,的数据。 * * **/ public static byte[] getImage(String urlpath) throws Exception { URL url = new URL(urlpath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(6 * 1000); // 别超过6秒。 if(conn.getResponseCode()==200){ InputStream inputStream=conn.getInputStream(); return readStream(inputStream); } return null; } /** * 读取数据 * 输入流 * * */ public static byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outstream=new ByteArrayOutputStream(); byte[] buffer=new byte[1024]; int len=-1; while((len=inStream.read(buffer)) !=-1){ outstream.write(buffer, 0, len); } outstream.close(); inStream.close(); return outstream.toByteArray();
}
}
android 网络编程--URL获取数据/图片相关推荐
- Android初学八之Android网络编程
目录 0 环境 1 目标 2 OkHttp框架的原理 3 界面展示 4 核心代码 4.1 UI设计 4.2 安装网络框架环境 4.3 使用网络框架连接后台数据并在APP中展示数据结果 5 代码仓库 6 ...
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关类 简介
Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api
Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...
- 【Android】Android网络编程概述
Android网络编程概述 原文来自:http://blog.csdn.net/kieven2008/article/details/8210737 首先,应该了解的几个问题: 1)Android平台 ...
- Android网络编程(八) 之 HttpURLConnection原理分析
1 使用回顾 我们在前面博文<Android网络编程(四) 之 HttpClient与HttpURLConnection>中已经对HttpURLConnection的使用进行过介绍.今天我 ...
- Kotlin第五章: android网络编程
1. Android网络编程 OkHttp OkHttp是一个高效的HTTP客户端,它的横空出世,让其他的网络请求框架都变得黯然失色. Retrofit Retrofit是一个基于OkHttp的RES ...
- Android 网络编程(二) HTTP协议解析
文章目录 引言 HTTP 简介 什么是HTTP,HTTPS 又是什么? HTTP 协议的特点 HTTP URL 格式 HTTP的报文 请求报文 请求行 请求报头 请求数据 响应保文 状态行 响应报头 ...
- Android网络编程 HttpUrlConnection HttpClient AsyncTask
前面几篇文章介绍了Android中常用的http框架:volley,所谓框架者无非就是对我们所需的一系列操作进行了封装,简化了我们的工作. 但万变不离其宗,我们还是需要掌握Android原生的网络操作 ...
- Android网络编程(一)
Android网络编程概述 首先,应该了解的几个问题: 1)Android平台网络相关API接口 a) java.net.*(标准Java接口) java.net.*提供与联网有关的类,包括流.数据包 ...
最新文章
- AI一分钟 | “芯片门”影响仍在,英伟达旗下多款芯片也遭遇“幽灵”漏洞影响;贾跃亭邀媒体试乘法乐第FF91,你怎么看?
- 二叉树中第二小的节点
- filestream 生成xml 文件时被如何让禁止转义_从Edgecam到PCDMIS,如何将工艺工程师的思想加入质量检测?...
- 使用Classycle验证类/包依赖关系
- 神经网络相关的笔试题目集合(一)
- Excel 数据导入到Access数据库,报错“找不到可安装的ISAM”的解决办法
- 012-Java有几种文件拷贝方式?哪一种最高效?
- 详解nullable、firstpos、lastpos和followpos的计算规则
- IP记录Linux所有用户操作日志的方法(附脚本)
- php ai,php能做AI智能吗
- 【实战分享】js生成word(docx),以及将word转成pdf解决方案分享
- Protel99SE教程(二)——PCB封装
- 十进制12345在32位计算机中的十六进制表示方法
- 恶意融资与上市公司的股权结构研究
- SQLyog的免费使用方式
- 大模型“研究源”告急:2026年高质量语言数据或将耗尽
- 软碟通UltraISO 9.65.3237官方注册版
- http://www.ibm.com/developerworks/cn/linux/l-cn-spidermonkey/index.html
- 关于 google voice 账号转移(新版google voice)
- 【DSP】DTMF 信号的编码和解码