版本更新2

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wj993257104/article/details/52046100
//网络

package test.bwie.com.gaoxuge20160701.utils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by gateway on 2016/7/1.
 */

public class NetWorkUtils {
    public static String getString(String path) {
        try {
            //创建URL对象
            URL url = new URL(path);
            //获取HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置连接超时时间
            conn.setConnectTimeout(5 * 1000);
            //设置读取超时时间
            conn.setReadTimeout(5000);
            //请求方式
            conn.setRequestMethod("GET");
            int code = conn.getResponseCode();
            if (code == 200) {
                //从流中读取数据
                InputStream inStream = conn.getInputStream();
                int len = -1;
                byte[] arr = new byte[1024];
                StringBuilder builder = new StringBuilder();
                while ((len = inStream.read(arr)) != -1) {
                    builder.append(new String(arr, 0, len));
                }
                return builder.toString();
            }

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


    public static InputStream getInputstreams(String path) {
        try {
            //穿件url
            URL url = new URL(path);
            //获取HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置连接超时时间
            conn.setConnectTimeout(5 * 1000);
            //读取超时时间
            conn.setReadTimeout(5000);
            conn.setRequestMethod("GET");
            int code = conn.getResponseCode();
            if (code == 200) {
                //获取流
                InputStream inStream = conn.getInputStream();

                //返回
                return inStream;
            }

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


阅读更多

没有更多推荐了,返回首页