您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > android网络通信 接收和发送数据详解
一、从网络上获取数据(图片、网页、XML、JSON等)1.从网络获取一张图片,然后显示在手机上[java]viewplaincopy1.①publicbyte[]getImageFromNet(){2.try{3.URLurl=newURL();4.HttpURLConnectionconn=(HttpURLConnection)url.openConnection();5.conn.setRequestMethod(GET);6.conn.setConnectTimeout(5*1000);7.conn.connect();8.InputStreaminStream=conn.getInputStream();9.byte[]data=readInputStream(inStream);//获取图片的二进制数据10.//FileOutputStreamoutStream=newFileOutputStream(360buy.jpg);11.//outStream.write(data);12.//outStream.close();13.returndata;14.}catch(Exceptione){15.e.printStackTrace();16.}17.}18.privatebyte[]readInputStream(InputStreaminStream)throwsIOException{19.ByteArrayOutputStreambyteOutputStream=newByteArrayOutputStream();20.byte[]buffer=newbyte[1024];21.intlen=-1;22.while((len=inStream.read(buffer))!=-1){23.byteOutputStream.write(buffer,0,len);24.}25.inStream.close();26.byte[]data=byteOutputStream.toByteArray();27.byteOutputStream.close();28.returndata;29.}②使用ImageView组件显示图片。③生成位图并设置到ImageView中[java]viewplaincopy1.Bitmapbitmap=BitmapFactory.decodeByteArray(data,0,data.length);2.imageView.setImageBitmap(bitmap);④在AndroidManifest.xml文件添加网络访问权限:[java]viewplaincopy1.uses-permissionandroid:name=android.permission.INTERNET/2.从网络获取指定网页的html代码,然后显示在手机上[java]viewplaincopy1.①publicStringgetHtmlCodeFromNet(){2.try{3.URLurl=newURL();4.HttpURLConnectionconn=(HttpURLConnection)url.openConnection();5.conn.setRequestMethod(GET);6.conn.setConnectTimeout(5*1000);7.conn.connect();8.InputStreaminStream=conn.getInputStream();9.byte[]data=readInputStream(inStream);10.StringhtmlString=newString(data,gb2312);11.System.out.println(htmlString);12.returnhtmlString;13.}catch(Exceptione){14.e.printStackTrace();15.}16.}②使用TextView组件显示网页代码ScrollView滚动条[java]viewplaincopy1.ScrollView2.android:layout_width=fill_parent3.android:layout_height=fill_parent4.TextView5.android:layout_width=fill_parent6.android:layout_height=wrap_content7.android:id=@+id/textView8./9./ScrollView③在AndroidManifest.xml文件添加网络访问权限:[java]viewplaincopy1.uses-permissionandroid:name=android.permission.INTERNET/3.从服务器上获取最新的视频资讯信息,该信息以XML格式返回给Android客户端,然后列表显示在手机上。最新资讯喜羊羊与灰太狼时长:60盗梦空间时长:120生化危机时长:100①开发web端,在此采用Struts2技术②设计显示界面,使用ListView③开发Android手机视频资讯客户端注意:不能使用127.0.0.1或者localhost访问在本机开发的web应用部分代码:[java]viewplaincopy1.publicListVideogetXMLLastVideos(StringurlPath)throwsException{2.URLurl=newURL(urlPath);3.HttpURLConnectionconn=(HttpURLConnection)url.openConnection();4.conn.setRequestMethod(GET);5.conn.setConnectTimeout(5*1000);6.conn.connect();7.InputStreaminStream=conn.getInputStream();8.returnparseXML(inStream);9.}[java]viewplaincopy1.privateListVideoparseXML(InputStreaminStream)throwsException{2.ListVideovideos=null;3.Videovideo=null;4.XmlPullParserparser=Xml.newPullParser();5.parser.setInput(inStream,UTF-8);6.inteventType=parser.getEventType();7.while(eventType!=XmlPullParser.END_DOCUMENT){8.switch(eventType){9.caseXmlPullParser.START_DOCUMENT:10.videos=newArrayListVideo();11.break;12.caseXmlPullParser.START_TAG:13.Stringname=parser.getName();14.if(video.equals(name)){15.video=newVideo();16.video.setId(newInteger(parser.getAttributeValue(0)));17.}18.if(video!=null){19.if(title.equals(name)){20.video.setTitle(parser.nextText());21.}elseif(timeLength.equals(name)){22.video.setTimeLength(newInteger(parser.nextText()));23.}24.}25.break;26.caseXmlPullParser.END_TAG:27.Stringpname=parser.getName();28.if(video.equals(pname)){29.videos.add(video);30.video=null;31.}32.break;33.default:34.break;35.}36.eventType=parser.next();37.}38.returnvideos;39.}④在AndroidManifest.xml文件添加网络访问权限:[java]viewplaincopy1.uses-permissionandroid:name=android.permission.INTERNET/4.从服务器上获取最新的视频资讯信息,该信息以JSON格式返回给Android客户端,然后列表显示在手机上。服务器端需要返回的JSON数据:[java]viewplaincopy1.[{id:1,title:aaa1,timeLength:50},{id:2,title:aaa2,timeLength:50},{id:3,title:aaa3,timeLength:50}][java]viewplaincopy1.publicListVideogetJSONLastVideos(StringurlPath)throwsException{2.URLurl=newURL(urlPath);3.HttpURLConnectionconn=(HttpURLConnection)url.openConnection();4.conn.setRequestMethod(GET);5.conn.setConnectTimeout(5*1000);6.conn.connect();7.InputStreaminStream=conn.getInputStream();8.byte[]data=StreamTools.readInputStream(inStream);9.Stringjson=newString(data);10.JSONArrayarray=newJSONArray(json);11.ListVideovideos=newArrayListVideo();12.for(inti=0;iarray.length();i++){13.JSONObjectitem=array.getJSONObject(i);14.intid=item.getInt(id);15.Stringtitle=item.getString(title);16.inttimeLength=item.getInt(timeLength);17.videos.add(newVideo(id,title,timeLength));18.}19.returnvideos;20.}二、通过HTTP协议提交文本数据(GET/POST)GET、POST、HttpClient1.通过GET方式提交参数给服务器:注意处理乱码(Android系统默认编码是UTF-8),提交的数据最大2K。①服务器端代码[java]viewplaincopy1.HttpServletRequestrequest=ServletActionContext.getRequest();2.//服务器端编码处理,先以ISO-8859-1编码得到二进制数据,然后使用UTF-8对数据重新编码3.byte[]data=request.getParameter(title).getBytes(ISO-8859-1);4.StringtitleString=newString(data,UTF-8);5.System.out.println(this.title==+titleString
本文标题:android网络通信 接收和发送数据详解
链接地址:https://www.777doc.com/doc-5372854 .html