资讯_HttpURLConnection详情

网络请求值HttpURLConnection
1.ANR以及网络判断
1.ANR的概念

在这里插入图片描述
2.如何避免ANR异常?
在这里插入图片描述
3.掌握网络连接的判断

public static boolean isNetworkHttp(Context context) {
        //写网络权限
        //注意参数是否为空
        if (context != null) {
            //getSystemService获取系统服务
            //获取连接管理器
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取网络状态
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            if (info != null) {
                //判断网络是否可用
                return info.isAvailable();
            }
        }
        return false;
    }

GET请求

public static String requestHttpUrl(String strUrl){

        try {
            //设置URL
            URL url = new URL(strUrl);
            //获取httpConnection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //设置get请求
            connection.setRequestMethod("GET");
            //设置连接主机超时时间
            connection.setConnectTimeout(5000);
            //设置从主机读取数据超时
            connection.setReadTimeout(5000);
            //获取请求码
            int code = connection.getResponseCode();
            //判断请求是否成功
            if (code== HttpURLConnection.HTTP_OK){
                //读取数据
                InputStream stream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"utf-8"));
                //拼接字符串
                StringBuilder builder = new StringBuilder();
                String str = "";
                while((str = reader.readLine())!=null){
                    //拼接字符串
                    builder.append(str);
                }
                //返回数据
                return builder.toString();
            }
            //关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

GET请求照片

public static Bitmap httpGETImage(String string) {
        try {
            //设置URL
            URL url = new URL(string);
            //得到HttpURLConnection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            //连接超时时间
            connection.setConnectTimeout(5000);
            //读取超时
            connection.setReadTimeout(5000);
            //获取请求码 一般情况下200是成功
            int code = connection.getResponseCode();
            //判断请求码是否 请求成功
            if (code == HttpURLConnection.HTTP_OK) {
                //得到数据
                InputStream stream = connection.getInputStream();
                // 如果是图片
                Bitmap bitmap = BitmapFactory.decodeStream(stream);

                return bitmap;
            }
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

POST请求

public static String httpPost(String str1, String str2) {

        try {
            //设置url
            URL url = new URL(str1);
            //获取HttpURLConnextion
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //提交模式
            connection.setRequestMethod("POST");
            //连接超时单位毫秒
            connection.setConnectTimeout(5000);
            //读取超时单位毫秒
            connection.setReadTimeout(5000);
            //发送Post请求必须设置如下两行
            connection.setDoOutput(true);
            connection.setDoInput(true);
            //获取URLConnection对象对应的输出流
            PrintWriter writer = new PrintWriter(connection.getOutputStream());
            //发送请求
            writer.write(str2);
            //关闭流
            writer.flush();
            //刷新
            //获取请求码
            int code = connection.getResponseCode();
            //判断是否成功
            if (code == 200) {
                //读取数据
                InputStream stream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "utf-8"));
                //拼接数符串
                StringBuilder builder = new StringBuilder();
                String s = "";
                //读取数据
                while ((s = reader.readLine()) != null) {
                    //拼接字符串
                    builder.append(s);
                }

                return builder.toString();
            }
            //关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

String,StringBbuff’er,StringBulder
String:不可变字符
Stringbuffer:可变字符串、效率低、线程安全
StringBulder:可变字符序列、效率高、线程不安全

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值