实验5网络访问与服务Word格式.docx
《实验5网络访问与服务Word格式.docx》由会员分享,可在线阅读,更多相关《实验5网络访问与服务Word格式.docx(8页珍藏版)》请在冰豆网上搜索。
1、创建项目
新建一个Android工程,目录结构如下图:
2、添加布局文件
修改main.xml布局文件,如下
<
?
xmlversion="
1.0"
encoding="
utf-8"
>
<
LinearLayoutxmlns:
android="
android:
orientation="
vertical"
layout_width="
fill_parent"
layout_height="
<
EditTextandroid:
wrap_content"
android:
id="
@+id/editText1"
text="
match_parent"
/>
LinearLayoutandroid:
android:
@+id/linearLayout3"
<
TextViewandroid:
@+id/textView2"
参数"
@+id/editText2"
source"
/LinearLayout>
@+id/linearLayout2"
@+id/textView1"
取值"
@+id/editText3"
2556800654"
@+id/linearLayout1"
Buttonandroid:
@+id/Button1"
android:
get"
@+id/Button2"
post"
<
ScrollViewxmlns:
scrollbars="
fadingEdge="
@+id/TextView01"
paddingTop="
5dip"
/>
/ScrollView>
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{
HttpPostrequest=newHttpPost(url);
List<
NameValuePair>
postParameters=newArrayList<
();
postParameters.add(newBasicNameValuePair(name,value));
UrlEncodedFormEntityformEntity=newUrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
@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