Android简易网络请求工具WebHttpHelper
2018-07-20 来源:open-open
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import android.text.TextUtils; import android.util.Log; public class WebHttpHelper { private static WebHttpHelper util; public static WebHttpHelper getInstance() { if (util == null) { util = new WebHttpHelper(); } return util; } private WebHttpHelper() { super(); } public InputStream getInputStream(String urlS, Map<String, String> param, boolean isGet) throws Exception { URL url = null; HttpURLConnection hc = null; StringBuffer request = new StringBuffer(); try { String get = "GET"; for (String key : param.keySet()) { request.append(key + "=" + param.get(key) + "&"); } String newUrl = urlS; if (!TextUtils.isEmpty(request.toString())) { newUrl = urlS + "?" + request.toString(); } Log.e("url", newUrl); if (!isGet) { get = "POST"; url = new URL(urlS); } else { url = new URL(newUrl); } hc = (HttpURLConnection) url.openConnection(); hc.setConnectTimeout(45000); // hc.setRequestProperty("Cookie", ConstUtil.COOKIEMESSAGE); hc.setRequestMethod(get); if (!isGet) { for (String key : param.keySet()) { hc.addRequestProperty(key, param.get(key)); } hc.setDoOutput(true); hc.getOutputStream().flush(); hc.getOutputStream().close(); } hc.setInstanceFollowRedirects(false); hc.setDoInput(true); } catch (Exception e) { e.printStackTrace(); } return hc.getInputStream(); } public String getString(String urlS, Map<String, String> param, boolean isGet, String encoding) { if (param == null) { param = new HashMap<String, String>(); } BufferedReader br = null; StringBuffer sb = new StringBuffer(); try { br = new BufferedReader(new InputStreamReader(getInputStream(urlS, param, isGet), encoding)); String temp = ""; while ((temp = br.readLine()) != null) { sb.append(temp); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇: java实现文件下载功能代码
下一篇:java 日期时间工具类
最新资讯
热门推荐