Simple android client servlet server interaction
The android client sends data to the server through GET. After the server obtains the data, it obtains the information in the database from the server and returns the data in JSON format.
1. Format of parameter passing in GET mode:
Http: // 127.0.0.1/AndroidService/android/upload? Title = aaa & timelength = 90 format
What is the parameter? Title = aaa & timelength = 90. Connect multiple parameters.
2. Connect to the server to send request parameters and obtain the data returned by the server. After the client obtains the data, it mainly parses JSON data.
/**
* Obtain server data
* @ 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 ();
// Character encoding conversion
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 data parsing
First, retrieve the JSON object, and then use the GET method to obtain the data in the JSON object in the form of key-value pairs.
The server is mainly a Servlet that processes submitted parameters and returns data through the doGet () and doPost () methods. Deploy the WEB Project to the Tomcat server:
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); comment out; otherwise, no data is always returned to the client
Req. setCharacterEncoding ("UTF-8 ");
Resp. setContentType ("text/html; charset = UTF-8 ");
Resp. setCharacterEncoding ("UTF-8 ");
// Query the server database and obtain the returned value
Infos = new JSONArray ();
PrintWriter out = resp. getWriter ();
// ServletOutputStream out = resp. getOutputStream ();
// Important !!! Encoding format !!!
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 ("Returned client data:" + infos. toString ());
// Write data to the response
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 );
}
}
Database operations on the server:
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 ");
// Query the server database and obtain the returned value
Infos = new JSONArray ();
PrintWriter out = resp. getWriter ();
// ServletOutputStream out = resp. getOutputStream ();
Try {
/* Byte [] titleByte = request. getParameter ("title"). getBytes ("iso-8859-1"); // obtain the binary data corresponding to the title Parameter
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 ("Returned client data:" + infos. toString ());
Out. write (infos. toString ());
// System. out. println ("Return client data 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 configuration:
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
NOTE: If 360 shared WIFI is used for testing, the IP address of the wireless Nic is used, not the IP address of the Ethernet.