Server code
[Java]
Package org. xiazdong. servlet;
Import java. io. IOException;
Import javax. servlet. ServletException;
Import javax. servlet. annotation. WebServlet;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
@ WebServlet ("/PrintServlet ")
Public class PrintServlet extends HttpServlet {
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = new String (request. getParameter ("name"). getBytes ("ISO-8859-1"), "UTF-8 ");
String age = new String (request. getParameter ("age"). getBytes ("ISO-8859-1"), "UTF-8 ");
System. out. println ("name:" + name + "\ n age:" + age );
}
Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Request. setCharacterEncoding ("UTF-8 ");
System. out. println ("name:" + request. getParameter ("name") + "\ n age:" + request. getParameter ("age "));
}
}
Android code
[Java]
Package org. xiazdong. network. submit;
Import java. io. OutputStream;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import java.net. URLEncoder;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private EditText name, age;
Private Button getbutton, postbutton;
Private OnClickListener listener = new OnClickListener (){
@ Override
Public void onClick (View v ){
Try {
If (getbutton = v ){
/*
* Because it is a GET request, you must add the request parameters to the URL and perform URL encoding.
* URL = http: // 192.168.0.103: 8080/Server/PrintServlet? Name = % E6 % 88% 91 & age = 20
* URL encoding is required because the browser automatically performs URL encoding when submitting
**/
StringBuilder buf = new StringBuilder ("http: // 192.168.0.103: 8080/Server/PrintServlet ");
Buf. append ("? ");
Buf. append ("name =" + URLEncoder. encode (name. getText (). toString (), "UTF-8") + "&");
Buf. append ("age =" + URLEncoder. encode (age. getText (). toString (), "UTF-8 "));
URL url = new URL (buf. toString ());
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setRequestMethod ("GET ");
If (conn. getResponseCode () = 200 ){
Toast. makeText (MainActivity. this, "GET submitted successfully", Toast. LENGTH_SHORT). show ();
}
Else Toast. makeText (MainActivity. this, "GET submission failed", Toast. LENGTH_SHORT). show ();
}
If (postbutton = v ){
/*
* For a POST request, the request parameters are placed in the Request body,
* Name = % E6 % 88% 91 & age = 12
*
**/
StringBuilder buf = new StringBuilder ();
Buf. append ("name =" + URLEncoder. encode (name. getText (). toString (), "UTF-8") + "&");
Buf. append ("age =" + URLEncoder. encode (age. getText (). toString (), "UTF-8 "));
Byte [] data = buf. toString (). getBytes ("UTF-8 ");
URL url = new URL ("http: // 192.168.0.103: 8080/Server/PrintServlet ");
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setRequestMethod ("POST ");
Conn. setDoOutput (true); // This sentence must be added if output is to be made.
OutputStream out = conn. getOutputStream ();
Out. write (data );
If (conn. getResponseCode () = 200 ){
Toast. makeText (MainActivity. this, "GET submitted successfully", Toast. LENGTH_SHORT). show ();
}
Else Toast. makeText (MainActivity. this, "GET submission failed", Toast. LENGTH_SHORT). show ();
}
}
Catch (Exception e ){
} Www.2cto.com
}
};
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Name = (EditText) this. findViewById (R. id. name );
Age = (EditText) this. findViewById (R. id. age );
Getbutton = (Button) this. findViewById (R. id. getbutton );
Postbutton = (Button) this. findViewById (R. id. postbutton );
Getbutton. setOnClickListener (listener );
Postbutton. setOnClickListener (listener );
}
}
Author: xiazdong