WANGLUOqingqiuGj
2017-11-05 18:43
141 查看
public class NetUtil {
private static String tag = "getNetJson";
public static String getNetJson(String urlString) {
try {
//url对象封装接口字符串
URL url = new URL(urlString);
//用url打开连接, 返回值我们用HttpURLConnection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(8000);//设置链接超时时间
int responseCode = urlConnection.getResponseCode(); //获取状态码
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//可拼接的字符串
StringBuilder stringBuilder = new StringBuilder();
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
stringBuilder.append(temp);
}
String jsonString = stringBuilder.toString();
return jsonString;
} else {
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
}
return "";
}
}
private static String tag = "getNetJson";
public static String getNetJson(String urlString) {
try {
//url对象封装接口字符串
URL url = new URL(urlString);
//用url打开连接, 返回值我们用HttpURLConnection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(8000);//设置链接超时时间
int responseCode = urlConnection.getResponseCode(); //获取状态码
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//可拼接的字符串
StringBuilder stringBuilder = new StringBuilder();
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
stringBuilder.append(temp);
}
String jsonString = stringBuilder.toString();
return jsonString;
} else {
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
}
return "";
}
}
相关文章推荐
- wangluoqingqiu diyipian
- wangluoqingqiu dierpian
- android jiancewangluozhangtai
- wangluofugai
- Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang
- http://www.wangluqing.com/
- Windows Server 2008 R2 配置AD(Active Directory)域控制器 -zhai zi wangluo
- Configure the AD FS server for claims-based authentication -zhai zi wangluo
- http://home.wangjianshuo.com/cn/20050616_ajax_eeaec.htm
- ASP [a-zA-Z0-9]无错N长度随机数函数 By shawl.qiu
- ASP Adodb.Stream 上传文件到服务器硬盘, 详细注解 By shawl.qiu
- Dr. An Wang and Wang Laboratories (An legend in IT history)
- Ajax 强制刷新页面 By shawl.qiu
- jscript ado && adox 应用的几个技巧 by shawl.qiu
- sqEditor HTML 编辑器 for .net v1.0 By shawl.qiu
- shawl.qiu c# .net 服务端表单验证类 FormValidator 1.1
- shawl.qiu Javascript 前景色背景色调色类 / BgColorScheme v1.0
- Javascript 动态添加表格行(Tr),表格单元(Td) By shawl.qiu 2007-9-7
- Huaiqing Wang的讲座:How to write and publish quality research papers
- wangkangming2008的博客