编译接口工具类
来源:互联网 发布:数据切片和切块 编辑:程序博客网 时间:2024/05/20 13:41
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) {
String jsonString = stringBuilder.toString();
return jsonString;
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
}
return "";
}
/**
* 获取网络图片
*
* @param urlString
* @return
*/
public static Bitmap getNetBitmap(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection httpurlConnection = (HttpURLConnection) url.openConnection();
httpurlConnection.setConnectTimeout(8000);//设置链接超时时间
int responseCode = httpurlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = httpurlConnection.getInputStream();
//BitmapFactory: android提供好的加工图片的工具类,
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} else {
//do nothing
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetBitmap: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetBitmap: " + e.toString());
}
return null;
}
}
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 {
//donothing
}} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetJson: " + e.toString());
}
return "";
}
/**
* 获取网络图片
*
* @param urlString
* @return
*/
public static Bitmap getNetBitmap(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection httpurlConnection = (HttpURLConnection) url.openConnection();
httpurlConnection.setConnectTimeout(8000);//设置链接超时时间
int responseCode = httpurlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = httpurlConnection.getInputStream();
//BitmapFactory: android提供好的加工图片的工具类,
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} else {
//do nothing
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(tag, "getNetBitmap: " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e(tag, "getNetBitmap: " + e.toString());
}
return null;
}
}
阅读全文
0 0
- 编译接口工具类
- 接口工具类
- http接口工具类
- [学习笔记]接口工具类
- 命令行编译 java 接口类
- 接口工具
- 编译工具
- 编译工具
- 编译工具
- 编译工具
- 编译工具
- Cloneable接口 Object类 工具类
- struts2实现装配接口工具类
- Collection接口和Collections集合工具类
- 供应链重要工具类和接口
- 公众平台通用接口工具类
- HTTP POST接口 发送数据工具类
- Java 接口使用,工具类的分析
- dns请求包
- 继承和初始化时,程序运行顺序的自己的理解。
- Android 加载高清巨图,无需剪裁压缩
- 大周总结
- 调用动态链接库*.so
- 编译接口工具类
- Java算法之直接插入排序
- linux性能监控工具-(显示系统整体资源使用情况-top命令)
- 旷视科技完成4.6亿美元C轮融资,打破商汤4.1亿美元单轮融资记录
- 解密DNA机器人“巨大的一小步”:血液循环中精准送达药物
- 3分钟学会sessionStorage用法
- 博世投资张翠波:自动驾驶需要突破芯片、传感器、深度学习算法等关键技术
- Google智能生态链的演进路径
- 任正非最新讲话透露:华为在加快开发统一的人工智能平台