An explanation of HTTP requests has been explained in my blog:
http://blog.csdn.net/xiazdong/article/details/7215296
I have encapsulated a helper class for HTTP requests in http://blog.csdn.net/xiazdong/article/details/7725867, so it is easy to send get, post requests;
such as Httprequestutil.sendgetrequest (); Is to send a GET request;
First, the core code
HTTP GET Core Code:
(1) String value = Urlencoder.encode (String value, "UTF-8");
(2) String Path = "http://../path?key=" +value;
(3) URL url = new URL (path);//URL encoding is required for URLs here;
(4) HttpURLConnection con = (httpurlconnection) url.openconnection ();
(5) Con.setrequestmethod ("GET");
(6) Con.setdooutput (true);
(7) OutputStream out = Con.getoutputstream ();
(8) Out.write (BYTE[]BUF);
(9) Int code = Con.getresponsecode ();
HTTP POST Core Code:
(1) String value = Urlencoder.encode (String value, "UTF-8");
(2) Byte[]buf = ("key=" +value). GetBytes ("UTF-8");
(3) String Path = "Http://../path";
(4) URL url = new URL (path);//URL encoding is required for URLs here;
(5) HttpURLConnection con = (httpurlconnection) url.openconnection ();
(6) Con.setrequestmethod ("POST");
(7) Con.setdooutput (true);
(8) OutputStream out = Con.getoutputstream ();
(9) Out.write (BYTE[]BUF);
(ten) int code = Con.getresponsecode ();
Second, get and post garbled solution method
GET:
Add in Doget:
String name = new String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "UTF-8");
POST:
Add in Dopost:
Request.setcharacterencoding ("UTF-8");
For more information, see my blog:
http://blog.csdn.net/xiazdong/article/details/7217022
Third, server-side code
[Java]View PlainCopy
- 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 ("ages");
- }
- }
Four, the Android terminal code
Join in Androidmanifest.xml:
[HTML]View PlainCopy
- <uses-permission android:name="Android.permission.INTERNET"/>
Mainactivity.java
[Java]View PlainCopy
- 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, the request parameter needs to be added to the URL, and URL encoding is also required
- * URL = http://192.168.0.103:8080/Server/PrintServlet?name=%E6%88%91&age=20
- * URL encoding is required here because URL encoding is automatically made when the browser commits
- * */
- 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 () = =){
- Toast.maketext (mainactivity. This, "get submitted successfully", Toast.length_short). Show ();
- }
- Else Toast.maketext (mainactivity. This, "get commit Failed", Toast.length_short). Show ();
- }
- if (Postbutton = = v) {
- /*
- * If it is a POST request, the request parameter is 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); //If you want to export, you must add this sentence
- OutputStream out = Conn.getoutputstream ();
- Out.write (data);
- if (conn.getresponsecode () = =){
- Toast.maketext (mainactivity. This, "get submitted successfully", Toast.length_short). Show ();
- }
- Else Toast.maketext (mainactivity. This, "get commit Failed", Toast.length_short). Show ();
- }
- }
- catch (Exception e) {
- }
- }
- };
- @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);
- }
- }
Getting Started with Android: sending HTTP GET and POST requests