您的位置:首页 > 其它

版本更新2

2016-07-27 15:40 363 查看
//网络

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: