Android界面切换网络通信Word格式文档下载.docx
《Android界面切换网络通信Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Android界面切换网络通信Word格式文档下载.docx(17页珍藏版)》请在冰豆网上搜索。
@Override
publicvoidonClick(Viewv){
//通过调用setContentView函数切换layout
setContentView(R.layout.login);
}
});
}
方法2、通过转换到另一个Activity,步骤如下
①建一个Activity类
②把该类注册到AndroidManifest.xml,如下
<
activityandroid:
name="
.LoginActivity"
>
/activity>
③在原来的Activity上面通过创建Intent类来进行Activity的切换,代码如下
Buttonbutton_activity=(Button)this.findViewById(R.id.button2);
button_activity.setOnClickListener(newOnClickListener(){
Intentintent=newIntent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
二、Android与服务器通信的方法有:
方法一:
http通信,以xml或者json为载体,相互通信数据。
Android对于http的网络通信,它提供了标准的java接口——httpURLConnection接口,以及apache的接口——httpclient接口。
其中我自己用的比较多的而是httpclient这个接口,因为它的功能更为丰富很有效。
同时http通信也分为post方式和get的方式,两个相比较的话,post传送的数据量比较大,安全性也比较高。
因此在做数据查询时,我会用get方式;
而在做数据添加、修改或删除时,我就用Post方式
以下是基于httpclient接口,用get和post的方式通信的代码。
(get方式)
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
TextViewtext=(TextView)this.findViewById(R.id.text_http);
Stringhttpurl="
http:
//10.0.2.2:
8080/SIM_SERVER/org/Org-list"
;
//①httpget连接对象
HttpGethttpRequest=newHttpGet(httpurl);
//②取得httpclient的对象
HttpClienthttpclient=newDefaultHttpClient();
try{
//③请求httpclient,取得httpResponse
HttpResponsehttpResponse=httpclient.execute(httpRequest);
//④判断请求是否成功if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//⑤取得返回的字符串
StringstrResult=EntityUtils.toString(httpResponse.getEntity());
text.setText(strResult);
}else{
text.setText("
请求失败"
);
}catch(ClientProtocolExceptione){
e.printStackTrace();
}catch(IOExceptione){
}
(post方式)
this.setContentView(R.layout.login);
//①http连接地址
Stringurl="
8080/SIM_SERVER/and/Android-test"
//②取得httppost连接对象
HttpPosthttpPost=newHttpPost(url);
//③使用NameValuePair来保存要传递的Post参数
List<
NameValuePair>
params=newArrayList<
();
//④添加要传递的参数
params.add(newBasicNameValuePair("
parentId"
"
0"
));
HttpEntityhttpentity;
//⑤设置字符集
httpentity=newUrlEncodedFormEntity(params,"
utf-8"
//⑥把字符集设置在请求request里面
httpPost.setEntity(httpentity);
//⑦取得httpclient
HttpClienthttpClient=newDefaultHttpClient();
//⑧请求发送,并获得response
HttpResponsehttpResponse=httpClient.execute(httpPost);
//⑨判断请求是否成功if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//⑩取得返回字符串
}catch(UnsupportedEncodingExceptione){
方法二:
socket通信
socket与http不同的是两个的协议不同,socket是面向TCP/UDP协议的,http通信时面向HTTP协议的。
因为我之前做的比较多的都是web方面的项目,相对而言对,对HTTP协议了解比较多。
这里就简单说一下socket的通信。
publicclassClientActivityextendsActivity{
privateTextViewtext=null;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
text=(TextView)this.findViewById(R.id.text_http);
Buttonbutton=(Button)this.findViewById(R.id.button_direct);
button.setOnClickListener(newOnClickListener(){
try{
//①新建客户端socket
Socketsocket=newSocket("
10.0.2.2"
54321);
//②取得输出流
PrintWriterout=newPrintWriter(
newBufferedWriter(
newOutputStreamWriter(
socket.getOutputStream())),true);
//③向服务端输出信息
out.print("
我是客户端"
out.flush();
//④取得输入流
BufferedReaderin=newBufferedReader(
newInputStreamReader(
socket.getInputStream()));
//⑤读取信息
Stringmsg=in.readLine();
if(msg==null){
text.setText("
}else{
text.setText(msg);
}
//⑥关闭流
out.close();
in.close();
}catch(UnknownHostExceptione){
e.printStackTrace();
}