Android http Communication

Source: Internet
Author: User

Android http Communication

* ** 1. Server Side
The simplest server receives the code and prints the data that passes through the two*

Package cn. 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;/*** Servlet implementation class MyServlet */@ WebServlet ("/MyServlet") public class MyServlet extends HttpServlet {private static final long serialVersionUID = 1L; /*** @ see HttpServlet # HttpServlet () */public MyServlet () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {// TODO Auto-generated method stub // insist on calling the post method this. doPost (request, response);}/*** @ see HttpServlet # doPost (HttpServletRequest request, response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub // obtain data String name = request. getParameter ("name"); String age = request. getParameter ("age"); // print the data System. out. println (name); System. out. println (age );}}

Sevlet


MyServlet
Cn. servlet. MyServlet


MyServlet
/Ss

2. Client
First, we define a thread.

Public class Thread1 extends Thread // the data comes from the layout text editing box String name; String age; String url; public Thread1 (String name, String age, String url) {this. name = name; this. age = age; this. url = url ;}

1) implementation of the get Method

Public void doGet () {try {// url = url + "? Name = "+ URLEncoder. encode (name, "UTF-8") + "& age =" + age; // convert to url object URL httpUrl = new URL (url ); // obtain the connection object HttpURLConnection conn = (HttpURLConnection) httpUrl. openConnection (); // sets the transmission method conn. setRequestMethod ("GET"); conn. setReadTimeout (5000); // read the input stream BufferedReader br = new BufferedReader (new InputStreamReader (conn. getInputStream (); String str; StringBuffer sb = new StringBuffer (); // put sb while (str = Br. readLine ())! = Null) {sb. append (str) ;}} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}

2) implementation of the post method

Public void doPost () {try {URL httpUrl = new URL (url); HttpURLConnection conn = (HttpURLConnection) httpUrl. openConnection (); conn. setRequestMethod ("POST"); conn. setReadTimeout (5000); // note that conn is used here. getoutputstream, instead of new, can be associated with OutputStream out = conn. getOutputStream (); String content = "name =" + name + "& age =" + age; out. write (content. getBytes (); BufferedReader br = new BufferedReader (new InputStrea MReader (conn. getInputStream (); String str; StringBuffer sb = new StringBuffer (); while (str = br. readLine ())! = Null) {sb. append (str) ;}} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}

Which method is executed in the run method uses the corresponding method

@Override    public void run() {        // TODO Auto-generated method stub        super.run();    doGet();//      doPost();    }

3) The layout of input data on the client is simply set to two edit text boxes and one button.

private EditText etName,etAge;    private Button btnReg;

4) button click events

BtnReg. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {// TODO Auto-generated method stub // change the IP address to your current String url = "http: // 192.168.191.1: 8080/serve/ss "; new Thread1 (etName. getText (). toString (), etAge. getText (). toString (), url ). start ();}});}

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.