NetUtil 工具类

class NetUtil {
    public static String getNetJson(String urlString) {
        try {
            URL url = new URL(urlString);//专门处理网络请求的一个类,把接口包装成url对象;
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();//获取请求网络的状态码
            if (responseCode == 200) {
                InputStream inputStream = urlConnection.getInputStream();//得到服务器返回的数据流;
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();//可拼接字符串;
                String temp = "";
                while ((temp = bufferedReader.readLine()) != null) {
                    stringBuilder.append(temp);

                }

                String result = stringBuilder.toString();
                Log.e("wzq--result", result);
                return result;
            } else {
                //do nothing
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.e("wzq", e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("wzq", e.toString());
        }
        return "";

    }


    // 请求网络返回bitmap
    public static Bitmap getNetBitmap(String urlBitmap) {
        try {
            URL url = new URL(urlBitmap);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200) {
                InputStream inputStream = urlConnection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;

            }else {
                //do nothing
                Log.e("wzq","请求图片状态码"+responseCode);
            }


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

        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hutool工具常用类包括类型转换工具类(Convert)、字符串工具类(StrUtil / StringUtils)、日期工具类(DateUtil)、数字工具类(NumberUtil)、数组工具类(ArrayUtil)、随机工具类(RandomUtil)、比较器工具类(ComparatorUtil)、多线程工具类(ThreadUtil)、IO流工具类(FileUtil)、集合工具类(CollUtil / CollectionsUtils)、正则工具类(ReUtil)、网络工具类NetUtil)、JSON工具类(JSONUtil)、系统信息工具类(SystemUtil)等等。这些工具类提供了一系列常用的方法和功能,能够帮助开发者更加方便地进行类型转换、字符串处理、日期操作、数字处理、数组操作、随机数生成、多线程管理、IO流操作、集合操作、正则表达式匹配、网络操作、JSON处理、系统信息获取等等。通过使用hutool工具类,开发者可以提高开发效率,减少代码量,提供更加稳定和高效的程序功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Hutool常用工具类](https://blog.csdn.net/abst122/article/details/124091375)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [hutool 工具类](https://download.csdn.net/download/LiHaoYang11/12153632)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [常用工具类 (三) : Hutool 常用工具类整理 (全)](https://blog.csdn.net/m0_37989980/article/details/126401041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值