HttpURLConnection工具类 获取图片+Json

来源:互联网 发布:java自学还是培训 编辑:程序博客网 时间:2024/06/17 12:45
//获取图片
public Bitmap getNetImage(String urlstring){    try {        URL url=new URL(urlstring);        HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();        int responseCode = urlConnection.getResponseCode();        if(responseCode==200){            InputStream inputStream = urlConnection.getInputStream();            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);            return  bitmap;        }else {            Log.e("ZKF","responseCode"+responseCode);        }    } catch (Exception e) {        e.printStackTrace();    }    return null;}//请求字符串public String getNetString(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 stringTemp="";            while((stringTemp=bufferedReader.readLine())!=null){                stringBuilder.append(stringTemp);            }            String result = stringBuilder.toString();            Log.e("ZKF","请求的字符串"+result);            return result;        }else {            Log.e("ZKF","responseCode"+responseCode);        }    } catch (Exception e) {        e.printStackTrace();    }    return null;}
原创粉丝点击