实验5网络访问与服务.docx
- 文档编号:236949
- 上传时间:2022-10-07
- 格式:DOCX
- 页数:8
- 大小:139.71KB
实验5网络访问与服务.docx
《实验5网络访问与服务.docx》由会员分享,可在线阅读,更多相关《实验5网络访问与服务.docx(8页珍藏版)》请在冰豆网上搜索。
实验指导(五)
——网络访问与服务
一、实验目的
本次实验的目的是让大家通过一些小例子,学习和掌握Android网络访问方法。
二、实验要求
1、了解手机WEB网站访问的编程方法
2、通过网络进行数据访问
三、实验步骤
项目界面:
使用HttpClient来提交或者获取server端的数据。
Get:
Post
1、创建项目
新建一个Android工程,目录结构如下图:
2、添加布局文件
修改main.xml布局文件,如下
xmlversion="1.0"encoding="utf-8"?
>
android=" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent"> layout_height="wrap_content" android: id="@+id/editText1" android: text=" android: layout_width="match_parent"/> layout_height="wrap_content" android: layout_width="match_parent" android: id="@+id/linearLayout3"> id="@+id/textView2" android: layout_height="wrap_content" android: layout_width="wrap_content" android: text="参数"/> layout_height="wrap_content" android: layout_width="match_parent" android: id="@+id/editText2" android: text="source"/> layout_height="wrap_content" android: layout_width="match_parent" android: id="@+id/linearLayout2"> id="@+id/textView1" android: layout_height="wrap_content" android: layout_width="wrap_content" android: text="取值"/> layout_height="wrap_content" android: layout_width="match_parent" android: id="@+id/editText3" android: text="2556800654"/> layout_height="wrap_content" android: layout_width="match_parent" android: id="@+id/linearLayout1"> id="@+id/Button1" android: text="get" android: layout_height="wrap_content" android: layout_width="wrap_content"/> id="@+id/Button2" android: text="post" android: layout_height="wrap_content" android: layout_width="wrap_content"/> android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: scrollbars="vertical" android: fadingEdge="vertical"> layout_width="fill_parent" android: layout_height="wrap_content" android: id="@+id/TextView01" android: paddingTop="5dip"/> 3、创建Activity publicclassslide8_3extendsActivity{ publicStringmyhttpget(Stringurl)throwsException{ Stringout=null; try{ HttpClientclient=newDefaultHttpClient(); HttpGetrequest=newHttpGet(url); HttpResponseresponse=client.execute(request); out=EntityUtils.toString(response.getEntity()); }catch(IOExceptione){ e.printStackTrace(); } returnout; } publicStringmyhttppost(Stringurl,Stringname,Stringvalue) throwsException{ Stringout=null; try{ HttpClientclient=newDefaultHttpClient(); HttpPostrequest=newHttpPost(url); List postParameters.add(newBasicNameValuePair(name,value)); UrlEncodedFormEntityformEntity=newUrlEncodedFormEntity( postParameters); request.setEntity(formEntity); HttpResponseresponse=client.execute(request); out=EntityUtils.toString(response.getEntity()); }catch(IOExceptione){ e.printStackTrace(); } returnout; } @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); finalTextViewtextView=(TextView)findViewById(R.id.TextView01); finalEditTexteditText=(EditText)findViewById(R.id.editText1); finalEditTextname=(EditText)findViewById(R.id.editText2); finalEditTextvalue=(EditText)findViewById(R.id.editText3); Buttonbtn=(Button)this.findViewById(R.id.Button1); btn.setOnClickListener(newButton.OnClickListener(){ publicvoidonClick(Viewv){ try{ textView.setText(myhttpget(editText.getText().toString() +"? "+name.getText().toString()+"=" +value.getText().toString())); }catch(Exceptione){ e.printStackTrace(); } } }); Buttonbtn2=(Button)this.findViewById(R.id.Button2); btn2.setOnClickListener(newButton.OnClick
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 网络 访问 服务