Android submit data to Javaweb server implementation login

Source: Internet
Author: User
Tags php server

Before learning Android to submit data to the PHP server did not succeed, after watching the video for two or three weeks, now finally realize the interaction with the server. Although the completion is not the PHP side, but in this process has learned a lot of things. Now let me show you my chart of achievement.

The idea is that there is a data in the server-side simulation database: username=123,password=123 users. If the data submitted by the Android is a user name and password are 123 data. The return succeeds or the failure is returned.

As the video of the tutorial is all about interacting with javaweb, I try to build a javaweb server. In this process, several problems have been encountered:

One, Tomcat and javaweb the number of digits inconsistent, 32-bit javaweb can not run on 64-bit Tomcat.

Second, the Tomcat default port number is 8080. But this port is occupied when it is running. Therefore, the port number is changed in the Web. xml file.

Solved the problem above javaweb is to write an interface to the Android side.

Create a Web project first, and then create a servlet.

Package Com.itcast.login;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Login extends HttpServlet {private static final long Serialversionuid = 1l;public Login () {super ();} public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { String username = request.getparameter ("username"); String Password = request.getparameter ("password"); SYSTEM.OUT.PRINTLN ("QQ number:" +username); System.out.println ("Password:" +password), if ("123". Equals (username) && "123". Equals (password)) { SYSTEM.OUT.PRINTLN ("Login Successful"); Response.getoutputstream (). Write ("Login succeeded". GetBytes ("Utf-8");} else {System.out.println ("Login Failed"); Response.getoutputstream (). Write ("Login failed". GetBytes ("Utf-8"));}} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}} 

The resulting access to the server using the Get method is:

The code on the Android side is:

 PackageCom.itheima.login;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;Importjava.net.ProtocolException;ImportJava.net.URL;ImportJava.net.URLEncoder; Public classLoginservice { Public Staticstring loginbypost (string username,string password) {Try{String path= "Http://192.168.1.101:8090/Java/servlet/Login"; URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setconnecttimeout (5000); Conn.setrequestmethod ("POST"); String Data= "Username=" +urlencoder.encode (username) + "&password=" +urlencoder.encode (password);            SYSTEM.OUT.PRINTLN (data); Conn.setrequestproperty ("Content=type", "application/x-wwww-form-urlencoded"); Conn.setrequestproperty ("Content-length", data.length () + ""); Conn.setdooutput (true); OutputStream OS=Conn.getoutputstream ();            Os.write (Data.getbytes ()); intCode =Conn.getresponsecode ();            SYSTEM.OUT.PRINTLN (code); if(Code = = 200) {InputStream is=Conn.getinputstream (); String text=Streamtools.readinputstream (IS); returntext; }Else {                return NULL; }                    } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("111111"); } Catch(protocolexception e) {System.out.println ("222222");        E.printstacktrace (); } Catch(IOException e) {System.out.println ("33333");        E.printstacktrace (); }        return NULL; }}

The result of the operation is:

Android submit data to Javaweb server implementation login

Related Article

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.