加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android客户端访问网络工具类

发布时间:2020-12-14 23:24:34 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Read

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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;  
  
/** 
 * 连接服务器 
 *  
 * @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 服务器IP 
     * @param userName GET方式传递参数用户名 
     * @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;// 线程安全  
        try {  
            url = new URL(str);//获得URL对象  
            try {  
                connection = (HttpURLConnection) url.openConnection();  
                connection.setConnectTimeout(3000);  
                connection.setRequestMethod("GET");//GET方式提交参数  
                connection.setDoOutput(true);//设置可以向服务器读写  
                connection.setDoInput(true);  
                //请求成功  
                if (connection.getResponseCode() == 200) {  
                    inputStream = connection.getInputStream();  
                    Reader reader = new InputStreamReader(inputStream,"UTF-8");  
                    //打包成字符流  
                    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 block  
                e.printStackTrace();  
            }  
        } catch (MalformedURLException e) {  
  
            e.printStackTrace();  
            //关闭流很重要  
        } finally {  
            if (inputStream != null) {  
                try {  
                    inputStream.close();  
                    inputStream = null;  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
  
            }  
            if (connection != null) {  
                connection.disconnect();  
                connection = null;  
            }  
  
        }  
        if (sb != null) {  
            return new String(sb);  
        }  
  
        return "服务器异常!";  
  
    }  
  
}  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读