HttpUrl解析工具类NetUtils
2017-11-01 19:55
316 查看
package com.example.day111exam_url.Utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Utils { public static Bitmap getimg(String urlstr) { try { //获取URL对象 URL url = new URL(urlstr); //打开连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置连接超时时间 connection.setConnectTimeout(5000); //获取状态码 int responseCode = connection.getResponseCode(); if (responseCode == 200) {//请求成功 InputStream inputStream = connection.getInputStream(); //使用bitmap解析图片 Bitmap image = BitmapFactory.decodeStream(inputStream); return image; } else { //do nothing } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public static String getmsg(String urlstr) { StringBuilder sb = new StringBuilder(); try { //获取URL对象 URL url = new URL(urlstr); //打开链接 HttpURLConnection connect = (HttpURLConnection) url.openConnection(); InputStream inputStream = connect.getInputStream(); //设置连接超时时间 connect.setConnectTimeout(5000); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //返回字符串 return sb.toString(); } }
相关文章推荐
- HttpURLConnection获取网络数据工具类
- 用HttpUrlconnection解析
- HttpUrlConnection 下的post, get , 和图片下载(工具类)
- HttpURLConnection请求数据及Gson解析
- Android HttpURLConnection数据获取并JSON解析
- 使用HttpURLConnection实现页面静态化工具类
- httpURLConnection获得网络数据解析json串
- 解析 HTTP(HttpURLConnection getResponseCode)
- 用HttpURLConnection 下载文件工具类
- JAVA解析URL工具类
- HttpURLConnection解析网络数据
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- HttpURLConnection解析数据
- HttpURLConnection下载数据,JSON解析数据, BaseAdapter 适配数据。
- httpUrlConnection完全解析
- 解析 HTTP(HttpURLConnection getResponseCode)
- [置顶] Android网络之HttpUrlConnection和Socket关系解析
- 工具类—使用AsyncTask+HttpURLConnection请求数据的工具类-MyTask
- HttpURLConnection工具类
- HTTP学习(一) URL之相对定位解析为绝对定位