请求android开发,http工具类

android开发工具  时间:2021-02-28  阅读:()

andr o id开发 http工具类public static HttpClient httpClient=new DefaultHttpClient();public static final String BASE_URL=

*get请求

*@param url

*发送请求的URL

*@r eturn服务器响应字符串

*@throw s Exc eptionpublic static String doGet(String url) throws Exc eption{//创建Http Get对象。

HttpGet get=new HttpGet(url);

//发送GET请求

HttpRes pons e httpRes pons e=httpClient.exec ute(get);//如果服务器成功地返回响应if(httpRespons e.getStatus Line().getStatus Code()==200) {//获取服务器响应字符串

HttpEnt ity entity=httpRe s p ons e.getEntity();

InputS tream c ontent=entity.getContent();return c onvertStreamTo String(c ontent);r eturn null;

*p os t请求

*@param url

*发送请求的URL

*@param params

*请求参数

*@r eturn服务器响应字符串

*@throw s Exc eptionpublic static String doPost(String url,Map String,String rawParams)throw s Exc eption{

//创建Http P o st对象。

HttpPost post=new HttpPost(url);

//如果传递参数个数比较多的话可以对传递的参数进行封装List NameValuePair params=new ArrayList NameValuePairfor(String key:rawParams.keySet()) {

//封装请求参数params.add(new BasicNameValuePair(key, rawParams.get(key)));//设置请求参数post.s etEntity(new UrlEnc odedFormEntity(params,utf-8));//发送P O ST请求

HttpRes pons e httpRes pons e=http Client.exec ute(po s t);

//如果服务器成功地返回响应if(httpRespons e.getStatus Line().getStatus Code()==200) {

//获取服务器响应字符串

HttpEnt ity entity=httpRe s p ons e.getEntity();

InputS tream c ontent=entity.getContent();

return c onvertStreamToString(c ontent);r eturn null;

*获取服务器的响应转换为字符串private static String convertStreamToString(InputStream is) {

BufferedReader reader=new BufferedReader(new InputStreamReader(is));StringBuilder sb=new StringBuilder();

String line=null;try{w hile((line=reader.readL ine()) !=null) {sb.append(line);

} catch(IOException e) {e.printStackTrac e();

}final ly{try{is.c lo s e();

} catch(IOException e) {e.printStackTrac e();return sb.toString();

[黑五]ProfitServer新加坡/德国/荷兰/西班牙VPS五折,不限流量KVM月付2.88美元起

ProfitServer已开启了黑色星期五的促销活动,一直到本月底,商家新加坡、荷兰、德国和西班牙机房VPS直接5折,无码直购最低每月2.88美元起,不限制流量,提供IPv4+IPv6。这是一家始于2003年的俄罗斯主机商,提供虚拟主机、VPS、独立服务器、SSL证书、域名等产品,可选数据中心包括俄罗斯、法国、荷兰、美国、新加坡、拉脱维亚、捷克、保加利亚等多个国家和地区。我们随便以一个数据中心为例...

wordpress外贸集团企业主题 wordpress高级推广外贸主题

wordpress外贸集团企业主题,wordpress通用跨屏外贸企业响应式布局设计,内置更完善的外贸企业网站优化推广功能,完善的企业产品营销展示 + 高效后台自定义设置。wordpress高级推广外贸主题,采用标准的HTML5+CSS3语言开发,兼容当下的各种主流浏览器,根据用户行为以及设备环境(系统平台、屏幕尺寸、屏幕定向等)进行自适应显示; 完美实现一套主题程序支持全部终端设备,保证网站在各...

CloudCone:$14/年KVM-512MB/10GB/3TB/洛杉矶机房

CloudCone发布了2021年的闪售活动,提供了几款年付VPS套餐,基于KVM架构,采用Intel® Xeon® Silver 4214 or Xeon® E5s CPU及SSD硬盘组RAID10,最低每年14.02美元起,支持PayPal或者支付宝付款。这是一家成立于2017年的国外VPS主机商,提供VPS和独立服务器租用,数据中心为美国洛杉矶MC机房。下面列出几款年付套餐配置信息。CPU:...

android开发工具为你推荐
苏州商标注册苏州如何申请商标注册?香港代理ip求香港澳门地区的代理IP谢谢分享暴风影音怎么截图请问如何在暴风影音上截图人人时光机怎么查看人人网的注册时间?湖南商标注册湖南商标注册代办公司伪静态伪静态和真静态哪种静态方式好镜像文件是什么什么是文件镜像?什么是镜像文件?唱吧电脑版官方下载电脑上可以安装唱吧吗?ios7固件下载ios7发布当天是否有固件下载ios7固件下载iOS7如何升级固件?
便宜虚拟主机 花生壳免费域名申请 kdata themeforest 全能主机 美国堪萨斯 息壤代理 购买国外空间 万网空间购买 下载速度测试 免费的域名 云服务器比较 php服务器 网站防护 国外免费网盘 mteam fatcow ncp 机柜尺寸 linuxvi 更多