2022年android网络连接 .pdf
《2022年android网络连接 .pdf》由会员分享,可在线阅读,更多相关《2022年android网络连接 .pdf(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、android 网络连接总结文章分类 :移动开发一: HttpURLConnection Java 代码URL sourceUrl; String fileName =; try sourceUrl = new URL( 网址 ); fileName = sourceUrl.getFile(); fileName = fileName.substring(fileName.lastIndexOf(/) + 1); fileName = /sdcard/+(new Date().getTime()+fileName; /*创建临时文件File myTempFile = File.createTe
2、mpFile(temfile, .+mp3);/ 文件扩展名记录临时文件名currentTempFilePath = myTempFile.getAbsolutePath(); */ FileOutputStream fos = new FileOutputStream(fileName); int read = 0; byte buffer = new byte512; HttpURLConnection conn = (HttpURLConnection) sourceUrl.openConnection(); conn.setDoInput(true); conn.connect();
3、int length = conn.getContentLength(); InputStream is = conn.getInputStream(); do read = is.read(buffer); if(read 0) fos.write(buffer, 0, read); while(read != -1); catch (MalformedURLException e) / TODO Auto-generated catch block e.printStackTrace(); return; catch (IOException e) / TODO Auto-generate
4、d catch block e.printStackTrace(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 11 页 - - - - - - - - - return; if(fileName !=) File file = new File(fileName); if (file.exists() /根据 filename,操作这个文件 URL sourceUrl; String fileName =; try sourceUrl = new URL( 网址
5、); fileName = sourceUrl.getFile(); fileName = fileName.substring(fileName.lastIndexOf(/) + 1); fileName = /sdcard/+(new Date().getTime()+fileName; /*创建临时文件File myTempFile = File.createTempFile(temfile, .+mp3);/ 文件扩展名记录临时文件名currentTempFilePath = myTempFile.getAbsolutePath(); */ FileOutputStream fos =
6、 new FileOutputStream(fileName); int read = 0; byte buffer = new byte512; HttpURLConnection conn = (HttpURLConnection) sourceUrl.openConnection(); conn.setDoInput(true); conn.connect(); int length = conn.getContentLength(); InputStream is = conn.getInputStream(); do read = is.read(buffer); if(read 0
7、) fos.write(buffer, 0, read); while(read != -1); catch (MalformedURLException e) / TODO Auto-generated catch block 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 11 页 - - - - - - - - - e.printStackTrace(); return; catch (IOException e) / TODO Auto-generated cat
8、ch block e.printStackTrace(); return; if(fileName !=) File file = new File(fileName); if (file.exists() /根据 filename,操作这个文件 Java 代码URL imageUrl = null; Bitmap bitmap = null; try /* new URL对象将网址传入*/ imageUrl = new URL(uriPic); catch (MalformedURLException e) e.printStackTrace(); try /* 取得连接*/ HttpURL
9、Connection conn = (HttpURLConnection) imageUrl .openConnection(); conn.connect(); /* 取得返回的InputStream */ InputStream is = conn.getInputStream(); mTextView1.setText(conn.getResponseCode()+=+conn.getResponseMessage(); /* 将 InputStream 变成 Bitmap */ bitmap = BitmapFactory.decodeStream(is); /* 关闭 InputSt
10、ream */ is.close(); catch (IOException e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 11 页 - - - - - - - - - e.printStackTrace(); URL imageUrl = null; Bitmap bitmap = null; try /* new URL对象将网址传入*/ imageUrl = new URL(uriPic); catch (MalformedURLException e) e
11、.printStackTrace(); try /* 取得连接*/ HttpURLConnection conn = (HttpURLConnection) imageUrl .openConnection(); conn.connect(); /* 取得返回的InputStream */ InputStream is = conn.getInputStream(); mTextView1.setText(conn.getResponseCode()+=+conn.getResponseMessage(); /* 将 InputStream 变成 Bitmap */ bitmap = Bitm
12、apFactory.decodeStream(is); /* 关闭 InputStream */ is.close(); catch (IOException e) e.printStackTrace(); 处理文字数据Java 代码/* 将 InputStream 转成 Reader */ BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream(); String inputLine; /* 图文件路径*/ String uriPic = ; /* 一行一行读取*/ while (in
13、putLine = in.readLine() != null) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 11 页 - - - - - - - - - uriPic += inputLine; /* 将 InputStream 转成 Reader */ BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream(); String inputLine; /* 图
14、文件路径*/ String uriPic = ; /* 一行一行读取*/ while (inputLine = in.readLine() != null) uriPic += inputLine; URLConnection 获取图片Java 代码URL aryURI = new URL(myImageURLposition); URLConnection conn = aryURI.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); Bitmap bm = BitmapFactory.decod
15、eStream(is); is.close(); imageView.setImageBitmap(bm); URL aryURI = new URL(myImageURLposition); URLConnection conn = aryURI.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); Bitmap bm = BitmapFactory.decodeStream(is); is.close(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - -
16、- - - - - - 名师精心整理 - - - - - - - 第 5 页,共 11 页 - - - - - - - - - imageView.setImageBitmap(bm); 注:URL 可以直接InputStream is = URL.openStream(); XML 的应用Java 代码URL url = new URL(ConstData.queryString+cityParamString); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser();
17、 XMLReader xr = sp.getXMLReader(); myHandler gwh = new myHandler(); xr.setContentHandler(gwh); InputStreamReader isr =new InputStreamReader(url.openStream(),GBK); InputSource is=new InputSource(isr); xr.parse(is); URL url = new URL(ConstData.queryString+cityParamString); SAXParserFactory spf = SAXPa
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年android网络连接 2022 android 网络 连接
限制150内