Package Com.yqq.loginclient.utils;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.reader;import java.net.HttpURLConnection; Import Java.net.malformedurlexception;import java.net.url;/** * Connect Server * * @author Yqq_coder * */public class Loginutils {public loginutils () {//TODO auto-generated constructor stub}/** * http://10.1.17.208:8080/LoginService/LoginServlet? username=lihua&password=123456 * http://localhost:8080/?userName=Lihua&passWord=123456 * @param IP Server IP * @ Param userName Get mode pass parameter user name * @param passWord password * @return */public static string connect (string IP, String userName, Str ing PassWord) {String str = "/http" + ip+ ": 8080/loginservice/loginservlet?username=" +username+ "&password=" + PassWord; URL Url=null;inputstream inputstream = null; HttpURLConnection connection = null; StringBuffer SB = null;//thread safe try {url = new URL (str);//Get URL object try {connection = (HttpURLConnection) Url.openconNection (); Connection.setconnecttimeout (+); Connection.setrequestmethod ("GET");// The Get method submits the parameter Connection.setdooutput (TRUE);//settings can read and write to the server Connection.setdoinput (TRUE);//The request succeeds if ( Connection.getresponsecode () = = () {InputStream = Connection.getinputstream (); Reader reader = new InputStreamReader (InputStream, "UTF-8");//packaged into a character stream bufferedreader BufferedReader = new BufferedReader ( Reader); String str1 = NULL;SB = new StringBuffer (); while ((str1 = Bufferedreader.readline ()) = null) {sb.append (STR1);}}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (Malformedurlexception e) {e.printstacktrace ();//close stream is important} finally {if (InputStream! = null) {try {Inputstream.close (); inputstream = null;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if (connection! = null) {connection.disconnect (); connection = null;}} if (SB! = null) {return new String (SB);} Return "Server Exception! ";}}
Android Client access Network Tools class