Http请求的工具类

/**
 *  用户手机是否有网络,判断的工具类
 */
public class NetWorkUtils {


    //判断网络是否连接
    public static boolean isNetWorkAvailable(Context context) {
        //网络连接管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //网络信息
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null) {
            return true;
        }


        return false;
    }


    //判断是否是wifi
    public static boolean isWifi(Context context) {
        //网络连接管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //网络信息
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }
    //判断是否是手机流量
    public static boolean isMobile(Context context) {
        //网络连接管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //网络信息
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * date:2017/7/27
 * function:网络请求的工具类(记得加网络权限)
         String urlPath ="http://huixinguiyu.cn/Assets/js/data.js";
         Map<String,Object> map=new HashMap<>();
         map.put("channelId",0);
         map.put("startNum",0);
         String result = UrlConnection.postUrlConnect(urlPath,map);
 */
public class UrlConnection {
    //post请求
    public static String postUrlConnect(String urlPath, Map<String, Object> map) {
        StringBuffer sbRequest = new StringBuffer();
        if (map != null && map.size() > 0) {
            for (String key : map.keySet()) {
                sbRequest.append(key + "=" + map.get(key) + "&");
            }
        }
        String request = sbRequest.substring(0, sbRequest.length() - 1);
        System.out.println(request);
        try {
            URL url = new URL(urlPath);
            HttpURLConnection httpurl = (HttpURLConnection) url.openConnection();
            httpurl.setRequestMethod("POST");
            //设置连接主机超时
            httpurl.setConnectTimeout(30000);
            //设置从主机读取数据超时
            httpurl.setReadTimeout(30000);
            httpurl.setDoInput(true);
            httpurl.setDoOutput(true);
            //读取数据
            OutputStream os = httpurl.getOutputStream();
            os.write(request.getBytes());
            //刷新
            os.flush();
            //判断返回值是否正确
            if (httpurl.getResponseCode() == 200) {
                InputStream in = httpurl.getInputStream();
                StringBuffer sb = new StringBuffer();
                //创建字符数组
                byte[] buff = new byte[1024];
                int len = -1;
                while ((len = in.read(buff)) != -1) {
                    sb.append(new String(buff, 0, len, "utf-8"));
                }
                //关闭流
                in.close();
                os.close();
                httpurl.disconnect();
                return sb.toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlPath;
    }
    //get请求
    public static String getUrlConnect(String urlPath) {
        try {
            //获取地址
            URL url = new URL(urlPath);
            //通过URL对象.openConnection,强转为(HttpURLConnection),的到HttpURLConnection对象
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//http.https
            //实际就是对HttpURLConnection进行初始化设置,设置请求模式:setRequestMethod
            httpURLConnection.setRequestMethod("GET");
            //设置连接超时时间.setConnectTimeout,注意单位是毫秒.
            httpURLConnection.setConnectTimeout(8000);
            //设置读取的超时时间,setReadTimeout,注意单位是毫秒.
            httpURLConnection.setReadTimeout(8000);
            //获取http返回的状态码..getResponseCode();
            int responseCode = httpURLConnection.getResponseCode();//200
            System.out.println(responseCode);
            //判断返回值
            if (responseCode == 200) {
                //获取服务器的返回的流,getInputStream();
                InputStream in = httpURLConnection.getInputStream();
                StringBuffer sb = new StringBuffer();
                byte[] buff = new byte[1024];
                int len = -1;
                while ((len = in.read(buff)) != -1) {
                    sb.append(new String(buff, 0, len, "utf-8"));
                }
                in.close();
                httpURLConnection.disconnect();
                return sb.toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * function:把字节流转换为字符串
 */
public class Tools {
    public static String getTextFromStream(InputStream is){
        byte[] b = new byte[1024];
        int len;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            while((len = is.read(b)) != -1){
                bos.write(b, 0, len);
            }
            //把流中的数据转换成字节数组的形式,然后用字节数组构造一个字符串
            byte[] bt = bos.toByteArray();
            bos.close();
            return new String(bt);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值