Simple Android Client Servlet server-side interaction

Source: Internet
Author: User

The Android client sends the data to the server via get, and after the server obtains the data, it obtains the information from the server and returns it in the JSON data format.

1, the format of the Get method to pass the parameter:

form of the http://127.0.0.1/AndroidService/android/upload?title=aaa&timelength=90  

What is the parameter? After the title=aaa&timelength=90. Multiple parameters with & connection.

2, the connection server sends the request parameter and obtains the data which the server returns, after the client obtains the data, mainly is some parsing to the JSON data.

/**
* Get data from the server
* @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 ();
The corresponding 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, the JSON object is taken out, and then the data inside the JSON object is removed in the form of a GET method key value pair.


The server is primarily a servlet that processes the submitted parameters through the Doget () and Dopost () methods and returns the data. The deployment of the Web project to the Tomcat server is OK as follows:

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); Comment out, otherwise there is always no data returned to the client
Req.setcharacterencoding ("UTF-8");
Resp.setcontenttype ("Text/html;charset=utf-8");
Resp.setcharacterencoding ("Utf-8");
Querying the server-side database and getting the return 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 ("Return the client's data:" +infos.tostring ());
Write data to a 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);
}


}


Server-side Operations database classes:

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");
Querying the server-side database and getting the return value
Infos=new Jsonarray ();
PrintWriter Out=resp.getwriter ();
Servletoutputstream out = Resp.getoutputstream ();
try {
/*byte[] Titlebyte = Request.getparameter ("title"). GetBytes ("iso-8859-1"); Get 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 ("Return the client's data:" +infos.tostring ());

Out.write (Infos.tostring ());
SYSTEM.OUT.PRINTLN ("Return data from Client 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);
}


}

Configuration of 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>


Note: If using the 360 shared WiFi test with the wireless network card IP, not Ethernet IP.



Simple Android Client Servlet server interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.