网络请求工具类_苏苏的小弟子

来源:互联网 发布:淘宝情趣用品 知乎 编辑:程序博客网 时间:2024/04/28 09:35
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * author:Ankey
 * E-mail:none
 * Date:2017-11-08
 */


public class NetUtil {


    private BufferedReader br;




    // 获取当前网络状态
    public int getNetState(Context context) {
        int stateType = -1;
        if (context != null) {
            // 获取系统服务管理类
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            // 获取网络活动信息对象
            NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                /**
                 * stateType:
                 * -1 无网络
                 * TYPE_WIFI = 1;
                 * TYPE_MOBILE = 0;
                 */
                stateType = activeNetworkInfo.getType();
            } else {
                // nothing
            }
        }
        return stateType;
    }


    // 通过
    public Bitmap getBitmapByNet(String urlStr) {
        HttpURLConnection urlConnection = null;
        InputStream inputStream =null;
        // 判断参数是否为空
        if (urlStr == null || urlStr.trim() == "") {
            Log.e("NetUtil", "getJsonByClient()参数为空 54行");
            return null;
        }
        try {
            URL url = new URL(urlStr);
            urlConnection = (HttpURLConnection) url.openConnection();
            // 打开网络连接
            urlConnection = (HttpURLConnection) url.openConnection();
            // 设置读取超时时间
            urlConnection.setReadTimeout(8000);
            // 设置连接超时时间
            urlConnection.setConnectTimeout(8000);
            // 获取响应码
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200) {// 请求成功
                // 获取字节流
                inputStream = urlConnection.getInputStream();
                // 通过Bitam工厂解析字节流转为Bitmap
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                Log.i("TAG","inputStream.close() 已经关闭了");
                if(inputStream!=null)inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public String getJsonByUrlConnection(String urlStr) {
        HttpURLConnection urlConnection = null;


        // 判断参数是否为空
        if (urlStr == null || urlStr.trim() == "") {
            Log.e("NetUtil", "getJsonByClient()参数为空");
            return null;
        }
        // 声明一个容器接收读取的数据
        StringBuffer sb = new StringBuffer();
        try {
            // 初始化URL
            URL url = new URL(urlStr);
            // 打开网络连接
            urlConnection = (HttpURLConnection) url.openConnection();
            // 设置读取超时时间
            urlConnection.setReadTimeout(8000);
            // 设置连接超时时间
            urlConnection.setConnectTimeout(8000);
            // 获取响应码
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200) {// 请求成功
                // 获取字节流
                InputStream inputStream = urlConnection.getInputStream();
                // 将字节输入流转成高效流
                br = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                return sb.toString().trim();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (br != null) br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
                if (urlConnection != null) urlConnection.connect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public String getJsonByClient(String urlStr){
        /*
         *   http://api.expoon.com/AppNews/getNewsList/type/1/p/1
         */
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(urlStr);
        try {
            HttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if(entity!=null) {
                return EntityUtils.toString(entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
原创粉丝点击