首页 > 代码库 > 简单的android客户端servlet服务端的交互
简单的android客户端servlet服务端的交互
android客户端通过GET方式发送数据到服务端,服务端获得数据后,从服务端获取数据库里的信息,并以JSON数据格式返回。
1、GET方式传参的格式:
http://127.0.0.1/AndroidService/android/upload?title=aaa&timelength=90的形式
参数是?后的title=aaa&timelength=90。多个参数用&连接。
2、连接服务器发送请求参数并获得服务器返回的数据,客户端获得数据后,主要是对JSON数据的一些解析。
/**
* 获得服务器的数据
* @param url
* @return
*/
public static String connect(URL url){
InputStream inputStream=null;
HttpURLConnection connection=null;
StringBuffer sb=null;
try {
connection=(HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
if(connection.getResponseCode()==200){
inputStream=connection.getInputStream();
//对应的字符编码转换
Reader reader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
String str = null;
sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null){
try {
inputStream.close();
inputStream=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(connection!=null){
connection.disconnect();
connection=null;
}
}
return new String(sb);
}
3、JSON数据解析
首先取出JSON对象,然后用GET方法按键值对的形式取出JSON对象里面的数据。
服务端主要是一个Servlet,通过doGet()和doPost()方法把提交的参数进行处理,并返回数据。把该WEB工程部署到Tomcat服务器里就OK了如下:
public class MyTest extends HttpServlet {
//private List<GpsInfo> infos;
private JSONArray infos;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(req, resp);注释掉,否则总是没有返回数据给客户端
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
//查询服务器端数据库并获得返回值
infos=new JSONArray();
PrintWriter out=resp.getWriter();
//ServletOutputStream out = resp.getOutputStream();
//重要!!!编码格式!!!
String s = new String(req.getParameter("name").getBytes("iso-8859-1"),"UTF-8");
System.out.println(s);
infos=DbUtis.getData(s);
//JSONObject object=new JSONObject();
System.out.println("返回客户端的数据:"+infos.toString());
//把数据写入响应
out.write(infos.toString());
out.flush();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
//doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(req, resp);
doGet(req,resp);
}
}
服务器端操作数据库的类:
public class MyTest extends HttpServlet {
//private List<GpsInfo> infos;
private JSONArray infos;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(req, resp);
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
//查询服务器端数据库并获得返回值
infos=new JSONArray();
PrintWriter out=resp.getWriter();
//ServletOutputStream out = resp.getOutputStream();
try {
/*byte[] titleByte = request.getParameter("title").getBytes("iso-8859-1"); //获得title参数对应的二进制数据
title = new String(titleByte, "UTF-8"); */
String s = new String(req.getParameter("name").getBytes("iso-8859-1"),"UTF-8");
System.out.println(s);
infos=DbUtis.getData(s);
//JSONObject object=new JSONObject();
System.out.println("返回客户端的数据:"+infos.toString());
out.write(infos.toString());
//System.out.println("返回客户端的数据2:"+out.toString());
out.flush();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
//doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(req, resp);
doGet(req,resp);
}
}
web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyTest</servlet-name>
<servlet-class>MyTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyTest</servlet-name>
<url-pattern>/MyTest</url-pattern>
</servlet-mapping>
</web-app>
注意:如果用用360共享WIFI测试的时候用的是无线网卡的IP,不是以太网的IP。
简单的android客户端servlet服务端的交互