网络判断+请求数据工具类

package com.example.myapplication2;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;

import com.google.gson.Gson;

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;
import java.net.URLConnection;

public class NetUtil {

    //內部私有修飾符
    private  static NetUtil instsance;
    public NetUtil(){

    }
//单例模式
    public static NetUtil getInstsance(){
    if (instsance==null){

        instsance=new NetUtil();
    }
        return instsance;
    }

    //判断网络状态
    public static boolean panduan(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo!=null&&activeNetworkInfo.isAvailable();
    }

    //定义接口
    public static interface CallBack<T>{
        void onSuccess(T t);
    }
    //异步请求
    public static void RequestData(final String str,final Class clazz,final CallBack callBack){

        new AsyncTask<String, Void, Object>() {
            @Override
            protected Object doInBackground(String... strings) {
			//将Api接口和实体类传走
            return reRequestData1(str,clazz);
            }

            @Override
            protected void onPostExecute(Object o) {
            //成功后返回一个实体类
                callBack.onSuccess(o);
            }
        }.execute(str);

    }
//Gson解析
//<T>为了避免强转所以给它个泛型
// T是实体类,由于不知道是哪,所以先定义一个。
//strtr是Api接口,因为谁也不知道接口是什么
// Class clazz是定一个实体类
    public static <T> T reRequestData1(String str, Class clazz) {

        T t = (T) new Gson().fromJson(RequestData2(str),clazz);

        return t;
    }
//得到成功后得到字符
//str是Api接口,因为谁也不知道接口是什么
    public static String RequestData2(String str) {

        String result="";
        try {
            URL url = new URL(str);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            int responseCode = urlConnection.getResponseCode();
            if (responseCode==200){

                result=getString(urlConnection.getInputStream()) ;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		//将字符返回出去
        return result;
    }
		//io流
    public static String getString(InputStream inputStream) throws IOException {
        StringBuilder stringBuilder =   new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){

            stringBuilder.append(tmp);
        }

        return stringBuilder.toString();
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_43814403/article/details/85002809