Android_HttpURLConnection GET、POST网络请求封装

用到一个IO 依赖

implementation 'io.janusproject.guava:guava:19.0.0'
import android.os.AsyncTask;
import android.text.TextUtils;

import com.google.common.io.ByteStreams;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/*Time:2019/4/13
 *Author:zhaozhiwei
 *Description:
 */public class HttpUtil {

    private static final HttpUtil Instance = new HttpUtil();

    //饿汉式
    public static HttpUtil getInstance() {
        return Instance;
    }

    public void Getasync(String url, final AsyncCallback asyncCallback) {
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... strings) {
                return GetDataHttp(strings[0]);
            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (!TextUtils.isEmpty(s)) {
                    asyncCallback.onSucceedP(s);
                }

            }
        }.execute(url);
    }

    public String GetDataHttp(String url) {
        HttpURLConnection httpURLConnection = null;
        try {
            URL url1 = new URL(url);
            httpURLConnection = (HttpURLConnection) url1.openConnection();
            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.setConnectTimeout(5000);
            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                //输入流
                InputStream inputStream = httpURLConnection.getInputStream();
                String s = new String(ByteStreams.toByteArray(inputStream));
                return s;
            }

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

    //Post请求
    public void PostAsync(final String url, final String username, final String pwd, final AsyncCallback callback) {
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... strings) {
                return PostDataHttp(strings[0],strings[1],strings[2]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (!TextUtils.isEmpty(s)){
                    callback.onSucceedP(s);
                }else{
                    callback.onFailP(503,"未请求到");
                }
            }
        }.execute(url,username,pwd);
    }

    private String PostDataHttp(String url, String name, String pwd) {
        HttpURLConnection httpURLConnection = null;
        try {
            URL url1 = new URL(url);
            httpURLConnection = (HttpURLConnection) url1.openConnection();
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setReadTimeout(5000);
            //请求头信息
            String body = "phone=" + URLEncoder.encode(name)+"&pwd="+URLEncoder.encode(pwd);

            httpURLConnection.getOutputStream().write(body.getBytes());

            if (httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
                InputStream inputStream = httpURLConnection.getInputStream();
                String s = new String(ByteStreams.toByteArray(inputStream));
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public interface AsyncCallback {
        void onSucceedP(String result);

        void onFailP(int i, String error);

    }
}

猜你喜欢

转载自blog.csdn.net/Android_Mr_Zhao/article/details/89300703