安卓网络编程——使用HttpURLConnection
来源:互联网 发布:梦幻西游mac安装打不开 编辑:程序博客网 时间:2024/05/16 06:41
HttpURLConnection
它是URLConnection的子类,可见HttpURLConnection在父类的基础上做了改进,从名称也就可以看出增加了一些用于操作Http资源的便捷方法。
实例-访问网络图片
1,效果图:
2,xml布局,包括ImageView 。Button。EditText控件
3,开发步骤:
1,获取控件对象2,button按钮设置监听器,方法体中启动线程(必须使用线程访问网络)。3,在线程run方法体中,获取并创建URL对象,判断不为空。 不为空,则取得HttpURLContent对象,在判断响应吗为200,若是则用输入流读取图片并封装到Bitmap对象。4,若上步成功,则使用handler通知界面主线程中ImageView设置此网络图片。
编程代码:
public class MainActivity extends Activity { private Button button =null; private ImageView image =null; private EditText edit =null; private Bitmap bitmap =null;//读取的网络图片 private Handler handler =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 InitView(); //添加按钮监听器 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //启动线程和网络资源交互,并使用handler与主线程界面交互 new Thread(new httprun()).start(); } }); //Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg.what ==1){ image.setImageBitmap(bitmap); } } }; } private void InitView() { // TODO Auto-generated method stub button=(Button)findViewById(R.id.btnView); image=(ImageView)findViewById(R.id.ivImage); edit=(EditText)findViewById(R.id.etImageUrl); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } //访问网络图片的线程 class httprun implements Runnable{ @Override public void run() { // TODO Auto-generated method stub String str_url =edit.getText().toString().trim(); if(str_url!=null) { try { URL url =new URL(str_url); HttpURLConnection hConnection =(HttpURLConnection) url.openConnection();//获得网络连接对象 hConnection.setConnectTimeout(5000); hConnection.setRequestMethod("GET"); if(hConnection.getResponseCode() ==200){ InputStream in=hConnection.getInputStream();//获得输入流对象 //通过bitmap来封装图片 bitmap =BitmapFactory.decodeStream(in); //通过handler通知主界面显示图片 handler.sendEmptyMessage(1); }else { Toast.makeText(MainActivity.this, "读取图片失败!", 1000).show(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { Toast.makeText(MainActivity.this, "url不能为空!", 3000).show(); } } }}
0 0
- 安卓网络编程——使用HttpURLConnection
- 网络编程:HttpURLConnection使用介绍
- 网络编程:HttpURLConnection使用介绍
- 安卓网络-冰山一角之httpurlconnection
- 安卓基于HttpURLConnection网络请求
- 安卓开发之网络请求HttpURLConnection
- Android之使用HttpURLConnection进行网络编程
- Java网络编程|HttpURLConnection的使用
- Android网络编程 HttpURLConnection,HttpClient,Asynchttpclient使用
- 使用HttpURLConnection访问网络——加载网络图片
- 网络编程(4)——HttpURLConnection/HttpClient基本实现
- 网络编程(5)——HttpURLConnection/HttpClient的区别
- 安卓基础:HttpURLConnection的使用
- 使用Http协议访问网络—使用HttpURLConnection
- Android网络编程---HttpURLConnection
- 网络编程之HttpURLConnection
- HttpURLConnection----Android网络编程
- 安卓1106_网络编程02get post 请求 httpclient httpurlconnection 开源提交 多线程下载 开源下载
- C#发送邮件代码
- 定时器Timer和定时任务TimerTask的用法
- ArcGIS 10.3.1正式发布
- 大三下学期期末对未来之路的思考
- 开源 java CMS - FreeCMS2.3会员个人资料
- 安卓网络编程——使用HttpURLConnection
- 图片内容管家 (把文字隐藏到图片里)
- IOS 加入购物车动画
- 为手机应用程序(Android、iOS、WindowsPhone)添加超声波通信技术
- Java笔记:环境搭建
- Joomla开发最佳实践
- Qt 编码中的某一字从 UTF-8 转换到 UNICODE 国际化时可能用到
- Rails Minitest style 指南
- android项目中注意的问题