封装HTTP请求

2018-10-10  本文已影响0人  勤学奋进小郎君

每次访问网址,需要配置网络连接的很多属性(e.i. 请求方法、连接超时时间、读取资源超时时间等),很麻烦,所以这里为了简化访问网址的过程,封装一个http请求的工具类

HttpUtil.java

需要注意的几个点

public class HttpUtil {

    //声明一个发起HTTP请求的方法
    public static void sendRequest(final String url){

        //开启子线程执行费事操作
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //要访问的网址对象
                    URL tagetUrl = new URL(url);
                    //打开连接通道,连接到远程网址对象
                    //然后将连接通道强制转化成HTTP协议
                    HttpURLConnection httpconnection = (HttpURLConnection) tagetUrl.openConnection();
                    //访问方法GET请求
                    httpconnection.setRequestMethod("GET");
                    //连接超时的时间
                    httpconnection.setConnectTimeout(10000);
                    //读取资源超时时间
                    httpconnection.setReadTimeout(10000);
                    //获取远程访问网址的资源的输入流对象
                    InputStream in =  httpconnection.getInputStream();
                    //读取输入流数据
                    //InputStreamReader: 将二进制数据转换成字符数据
                    //BufferedReader: 将字符数据转化成文本数据
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    //不断的将BufferedReader对象中将数据写入字符串对象中
                    String tmpstring;
                    StringBuilder stringBuilder = new StringBuilder();
                    while ((tmpstring = bufferedReader.readLine()) != null){
                        stringBuilder.append(tmpstring);
                    }
                    Log.e("数据", stringBuilder.toString());

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        
    }
}

MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //简单的访问网站
        new HttpUtil().sendRequest("http://360.net/");
    }
public interface callbackfuc {
     void onFinish(String string);
     void onError(String string);
}

``

public class HttpUtil {

    //声明一个发起HTTP请求的方法
    public static void sendRequest(final String url, final callbackfuc callbackfucer){

        //开启子线程执行费事操作
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //要访问的网址对象
                    URL tagetUrl = new URL(url);
                    //打开连接通道,连接到远程网址对象
                    //然后将连接通道强制转化成HTTP协议
                    HttpURLConnection httpconnection = (HttpURLConnection) tagetUrl.openConnection();
                    //访问方法GET请求
                    httpconnection.setRequestMethod("GET");
                    //连接超时的时间
                    httpconnection.setConnectTimeout(10000);
                    //读取资源超时时间
                    httpconnection.setReadTimeout(10000);
                    //获取远程访问网址的资源的输入流对象
                    InputStream in =  httpconnection.getInputStream();
                    //读取输入流数据
                    //InputStreamReader: 将二进制数据转换成字符数据
                    //BufferedReader: 将字符数据转化成文本数据
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    //不断的将BufferedReader对象中将数据写入字符串对象中
                    String tmpstring;
                    StringBuilder stringBuilder = new StringBuilder();
                    while ((tmpstring = bufferedReader.readLine()) != null){
                        stringBuilder.append(tmpstring);
                    }
                    Log.e("数据", stringBuilder.toString());
                    //是否传入这个接口对象
                    if (callbackfucer != null){
                        //将相应数据作为参数传给回调函数
                        callbackfucer.onFinish(stringBuilder.toString());
                        Log.e("ONfinish", stringBuilder.toString())
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        HttpUtil.sendRequest("http://www.80la.cc/", new callbackfuc() {
            //在回调函数这里自定义处理相应数据
            @Override
            public void onFinish(String string) {
            }
            @Override
            public void onError(String string) {
            }
        });

    }
上一篇下一篇

猜你喜欢

热点阅读