网络请求 以及网络状态的工具类

public class Httpcon {

//    public  static String gethttp(Context context,String mpath,String mRam){}
    public  static  String gethttp(Context context, String mPath, String mRam){
        String messages = "";
        try {
            //http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt
            //  String  mPath="http://47.95.253.123/boot/api/findKtcon?pageNum="+page+"&pageSize="+pageSize+"";
//            String  mPath="http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt";
            URL murl = new URL(mPath);
            Log.i("shop", "doInBackground: "+mPath);
            HttpURLConnection connection = (HttpURLConnection) murl.openConnection();
            //设置请求发送
            connection.setRequestMethod(mRam);
            //请求超时
            connection.setConnectTimeout(5 * 1000);
            connection.setReadTimeout(5 * 1000);
            //数据流处理
            if (connection.getResponseCode() == 200) {
                //获取输入流
                InputStream inputStream = connection.getInputStream();
                //读取输入流
                byte[] b = new byte[1024 * 512]; //定义一个byte数组读取输入流
                ByteArrayOutputStream baos = new ByteArrayOutputStream(); //定义缓存流来保存输入流的数据
                int len = 0;
                while ((len = inputStream.read(b)) > -1) {  //每次读的len>-1 说明是是有数据的
                    baos.write(b, 0, len);  //三个参数  输入流byte数组   读取起始位置  读取终止位置
                }
                messages = baos.toString();
                inputStream.close();
                connection.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return messages;

    }

 

//网络状态
    public static boolean getnet(Context context){
        ConnectivityManager manger = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manger.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return info.isConnected();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值