Android client network access tools and android tools

Source: Internet
Author: User

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! ";}}

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.