- HttpURLCnnection获取网络流
- AsyncHttpClient获取网络流
- okhttp获取网络流(推荐使用,效率更高)
方法一:HttpURLCnnection
- 工具类:HttpUtils
public class HttpUtils { public HttpUtils() { // TODO Auto-generated constructor stub } public static String getJsonContent(String url_path) { try { URL url = new URL(url_path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setConnectTimeout(3000); connection.setRequestMethod("GET"); connection.setDoInput(true); int code = connection.getResponseCode(); if (code == 200) { return changeInputStream(connection.getInputStream()); } } catch (Exception e) { // TODO: handle exception } return ""; } private static String changeInputStream(InputStream inputStream) { // TODO Auto-generated method stub String jsonString = ""; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int len = 0; byte[] data = new byte[1024]; try { while ((len = inputStr
android获取网络输出字符串流的三种方式
最新推荐文章于 2022-10-09 19:27:11 发布