工作类

我的工作类 :MyNewWork

public class MyNewWork {
    //网络获取Json
    public static String getUrlString(String urlString){

        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            //获取请求码
            int responseCode = urlConnection.getResponseCode();
            //判断请求吗是否==200
            if (responseCode == 200) {
                InputStream inputStream = urlConnection.getInputStream();
                //按行读取
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                //
                String qq="";
                String ww="";
                //
                while((qq=bufferedReader.readLine())!=null){
                    ww+=qq;
                }
                //返回数据
                return ww;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    //判断是否有网
    /*public static boolean isConn(Context context){
        boolean bisConnFlag=false;
        ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Contextext.CONNECTIVITY_SERVICE);
        NetworkInfo network = conManager.getActiveNetworkInfo();

        if(network!=null){
            bisConnFlag = network.isAvailable();
        }
        return bisConnFlag;
    }
*/

    public static  boolean isConn(Context context){
        Boolean bisconnFiag = false;
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        if (activeNetworkInfo != null) {
            bisconnFiag = activeNetworkInfo.isConnected();
        }

        return bisconnFiag;
    }



    //网络图片
    public static Bitmap getBitMap(String mUrl){

        try {
            URL url = new URL(mUrl);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200) {
                InputStream inputStream = urlConnection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                return  bitmap;
            }else{
                Log.e("gxq", "responseCode---tupian: "+responseCode );
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

}

//网络获取Json

 public static String getNetjson(String urlString) {

        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");//若果是get请求可以不用配置; 其他请求必须配置
            urlConnection.setConnectTimeout(8000);//设置链接超时间

            InputStream inputStream = urlConnection.getInputStream();//获取网络返回的输入流;

            //可拼接的字符串
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp = "";
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuilder.append(temp);
                temp = "";
            }
            //这个是网络获取的数据
            String data = stringBuilder.toString();
            Log.e(tag, "getData: " + data);
            return data;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
   //网络获取Json
    public static String getUrlString(String urlString){

        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            //获取请求码
            int responseCode = urlConnection.getResponseCode();
            //判断请求吗是否==200
            if (responseCode == 200) {
                InputStream inputStream = urlConnection.getInputStream();
      			  //按长度读取
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                //数组
                byte[] bytes = new byte[1024];
                //初始长度
                int length = 0;
                while ((length = stream.read(bytes)) != -1) {
                    byteArrayOutputStream.write(bytes, 0, length);
                }
				
                String s = byteArrayOutputStream.toString();
                return s;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值