public static String getjson(String urlStirng){ try { URL url = new URL(urlStirng); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream = urlConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer stringBuffer = new StringBuffer(); String temp=""; while ((temp=bufferedReader.readLine())!=null){ stringBuffer.append(temp); } String s = stringBuffer.toString(); return s; }else{ } } catch (Exception e) { e.printStackTrace(); } return ""; } public static Bitmap getimg(String urlimg){ try { URL url = new URL(urlimg); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream = urlConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; }else{ } } catch (Exception e) { e.printStackTrace(); } return null; } public static String getJsonByHttpClientGet(String jsonUrl) { //获取httpclient对象 DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); //准备一个get请求 // HttpGet httpGet = new HttpGet(jsonUrl); HttpPost httpPost = new HttpPost(jsonUrl); try { //得到服务器返回的数据; HttpResponse response = defaultHttpClient.execute(httpPost); //得到状态码 int statusCode = response.getStatusLine().getStatusCode(); if(statusCode ==200){ //entiry 里面封装的数据; HttpEntity entity = response.getEntity(); //这个result就是json字符串,剩下的就是解析工作了; String result = EntityUtils.toString(entity); return result; //Log.e("===", "result: "+result ); } } catch (IOException e) { e.printStackTrace(); } return null; }
NetUtil
最新推荐文章于 2019-06-11 14:56:12 发布