网上有很多例子来演示Android客户端和服务器端数据如何实现交互不过这些例子大多比较繁杂,对于初学者来说这是不利的,现在介绍几种代码简单、逻辑清晰的交互例子,本篇博客介绍第四种:
一、服务器端:
代码1:添加名为“AndroidServerServlet.java”的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
com.ghj.packageofservlet;
import
java.io.IOException;
import
java.io.PrintWriter;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class
AndroidServerServlet
extends
HttpServlet {
private
static
final
long
serialVersionUID = 6792396567928634227L;
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType(text/plain; charset=UTF-
8
);
request.setCharacterEncoding(UTF-
8
);
System.err.println(request.getParameter(clientData));
PrintWriter printWriter = response.getWriter();
printWriter.print(您好Android客户端!);
printWriter.flush();
printWriter.close();
}
}
|
代码2:修改名为“web.xml”的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!--?xml version=
1.0
encoding=UTF-
8
?-->
<web-app http:=
""
java.sun.com=
""
javaee=
""
ns=
""
version=
"2.5"
web-app_2_5.xsd=
""
xml=
""
xmlns=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation=
"http://java.sun.com/xml/ns/javaee"
>
<servlet>
<servlet-name>AndroidServerServlet</servlet-name>
<servlet-
class
>com.ghj.packageofservlet.AndroidServerServlet</servlet-
class
>
</servlet>
<servlet-mapping>
<servlet-name>AndroidServerServlet</servlet-name>
<url-pattern>/AndroidServerServlet</url-pattern>
</servlet-mapping>
</web-app>
|
二、Android手机客户端:
代码1:下载名为android-async-http-1.4.6.jar的jar文件,下载地址:http://download.csdn.net/download/wangshuxuncom/8116169
代码2:添加名为“AndroidClientActivity.java”的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package
com.example.androidclient;
import
org.apache.http.Header;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.Toast;
import
com.loopj.android.http.AsyncHttpClient;
import
com.loopj.android.http.AsyncHttpResponseHandler;
import
com.loopj.android.http.RequestParams;
public
class
AndroidClientActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.android_client);
Button sendButton = (Button) findViewById(R.id.send_button);
sendButton.setOnClickListener(
new
OnClickListener(){
@Override
public
void
onClick(View v) {
RequestParams requestParams =
new
RequestParams();
requestParams.add(clientData, 您好服务器端!);
new
AsyncHttpClient().post(http:
//172.16.99.207:8080/AndroidServer/AndroidServerServlet, requestParams, new AsyncHttpResponseHandler() {
@Override
public
void
onSuccess(
int
statusCode, Header[] headers,
byte
[] responseBody) {
if
(statusCode ==
200
){
Toast.makeText(AndroidClientActivity.
this
,
new
String(responseBody), Toast.LENGTH_LONG).show();
}
}
@Override
public
void
onFailure(
int
statusCode, Header[] headers,
byte
[] responseBody, Throwable error) {
Toast.makeText(AndroidClientActivity.
this
, 没有获取到Android服务器端的响应!, Toast.LENGTH_LONG).show();
}
});
}
});
}
}
|
代码3:添加名为“android_client.xml”的文件