httpUrlConnection
来源:互联网 发布:plc编程100例下载 编辑:程序博客网 时间:2024/06/07 01:47
//网络获取文字信息public static String getJsonByHttpUrlConnection(String urlString) { try { URL url = new URL(urlString); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 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(); } catch (IOException e) { e.printStackTrace(); } return "";}//网络获取图片信息public static Bitmap getNetBitmap(String urlBitmap){ try { URL url = new URL(urlBitmap); 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(); } catch (IOException e) { e.printStackTrace(); } return null;}
阅读全文
0 0
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- JVM调优:跟踪三个月后的最终调试结果
- 信号与频谱
- 【Excel-2010】空值替换
- JAVA中注释简介
- Qt之如何识别小键盘(数字键盘)
- httpUrlConnection
- android DrawerLayout 实现侧滑菜单 知识整理(二)
- 算法导论(五)——树
- “整数序列” 中 Presentation Error
- MFC公共控件
- A+B for Input-Output Practice (II)
- CSDN-markdown编辑器的使用
- _init_()函数作用
- 当UIScrollView遇上SnapKit