標籤:java 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服務端的互動