Android uses the get method to submit data to the server, androidget
First, build a simulated web server and create a dynamic web project. The servlet code is as follows:
Package com. wuyudong. web; 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; import org. apache. catalina. user;/*** Servlet implementation class LoginServlet */@ WebServlet ("/LoginServlet") public class LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L; /*** @ see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse * response) */protected void doGet (HttpServletRequest request, incluresponse) throws ServletException, IOException {String username = request. getParameter ("username"); String password = request. getParameter ("password"); System. out. println ("username:" + username); System. out. println ("username:" + password); if ("wuyudong ". equals (username) & "123 ". equals (password) {response. getOutputStream (). write ("login success ". getBytes ();} else {response. getOutputStream (). write ("login failed ". getBytes () ;}}/*** @ see HttpServlet # doPost (HttpServletRequest request, response * response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub }}
Create a new jsp page
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Create an android project with the following page layout:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <EditText android: id =" @ + id/et_name "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: hint = "Enter the username" android: inputType = "text"/> <EditText android: id = "@ + id/et_pwd" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "enter the password" android: inputType = "textPassword"/> <Button android: onClick = "login" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "login"/> </LinearLayout>
The Code is as follows:
Package com. wuyudong. loginclient; import java. io. byteArrayOutputStream; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import android. OS. bundle; import android. app. activity; import android. text. editable; import android. text. textUtils; import android. view. menu; import android. view. view; import android. widget. editText; import android. widget. toast; public class MainActivity ext Ends Activity {private EditText et_name; private EditText et_pwd; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_name = (EditText) findViewById (R. id. et_name); et_pwd = (EditText) findViewById (R. id. et_pwd);} public void login (View view) {String name = et_name.getText (). toString (). trim (); String pwd = e T_pwd.getText (). toString (). trim (); if (TextUtils. isEmpty (name) | TextUtils. isEmpty (pwd) {Toast. makeText (this, "the user name and password cannot be blank", 0 ). show ();} else {// simulate an http request and submit data to the Server String path = "http: // 169.254.168.71: 8080/web/LoginServlet? Username = "+ name +" & password = "+ pwd; try {URL url = new URL (path); // 2. create an http connection HttpURLConnection conn = (HttpURLConnection) url. openConnection (); // 3. set some request methods conn. setRequestMethod ("GET"); // note that uppercase conn is required for GET word subtitles. setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 "); int code = conn. getResponseCode (); // The Server Response code 200 OK // 404 page cannot be found /// 503 internal server error if (code = 200) {InputStream is = conn. getInputStream (); // converts the content of is TO ByteArrayOutputStream bos = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len =-1; while (len = is. read (buffer ))! =-1) {bos. write (buffer, 0, len);} String result = new String (bos. toByteArray (); is. close (); Toast. makeText (this, result, 0 ). show ();} else {Toast. makeText (this, "request failed, cause of failure:" + code, 0 ). show () ;}} catch (Exception e) {e. printStackTrace (); Toast. makeText (this, "request failed, please check the logcat log console", 0 ). show ();}}}}
Add permission: android. permission. INTERNET
After running the project, click the button and a message indicating an error is displayed:
06-18 21:08:16. 237: W/System. err (7417): android. OS. NetworkOnMainThreadException
Android forces Versions later than android 4.0 not to check the main thread for network access.
Solution: add the following code in onCreate:
StrictMode. ThreadPolicy policy = new StrictMode. ThreadPolicy. Builder (). permitAll (). build ();
StrictMode. setThreadPolicy (policy );
Done