Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 簡單的android客戶端servlet服務端的交互

簡單的android客戶端servlet服務端的交互

編輯:關於Android編程

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 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 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的配置:


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">


index.jsp






MyTest
MyTest




MyTest
/MyTest




注意:如果用用360共享WIFI測試的時候用的是無線網卡的IP,不是以太網的IP。



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved