Android client network access tools and android tools
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 to the 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 method Pass Parameter userName * @ param passWord * @ return */public static String connect (String ip, string userName, String 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 security try {url = new URL (str); // obtain the URL object try {connection = (HttpURLConnection) url. openConnection (); connection. setConnectTimeout (3000); connection. setRequestMethod ("GET"); // submit the connection parameter in GET mode. setDoOutput (true); // you can read and write data to the server. setDoInput (true); // request success I F (connection. getResponseCode () = 200) {inputStream = connection. getInputStream (); Reader reader = new InputStreamReader (inputStream, "UTF-8"); // package it into a bytes 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 (); // It is important to close the stream} 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! ";}}