简单的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。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值