资讯_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:可变字符序列、效率高、线程不安全

你可能感兴趣的:(资讯)