Android Client access Network Tools class

Source: Internet
Author: User

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

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.