请求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();

GeorgeDatacenter:洛杉矶/达拉斯/芝加哥/纽约vps云服务器;2核/8GB/250GB/2TB流量/1Gbps端口,$84/年

georgedatacenter怎么样?GeorgeDatacenter是一家2017年成立的美国商家,正规注册公司(REG: 10327625611),其实是oneman。现在有优惠,有几款特价VPS,基于Vmware。支持Paypal付款。GeorgeDatacenter目前推出的一款美国vps,2核/8GB内存/250GB NVMe空间/2TB流量/1Gbps端口/Vmware/洛杉矶/达拉...

cyun29元/月,香港CN2 GIA云服务器低至起;香港多ip站群云服务器4核4G

cyun怎么样?cyun蓝米数据是一家(香港)藍米數據有限公司旗下品牌,蓝米云、蓝米主机等同属于该公司品牌。CYUN全系列云产品采用KVM架构,SSD磁盘阵列,优化线路,低延迟,高稳定。目前,cyun推出的香港云服务器性价比超高,香港cn2 gia云服务器,1核1G1M/系统盘+20G数据盘,低至29元/月起;香港多ip站群云服务器,16个ip/4核4G仅220元/月起,希望买香港站群服务器的站长...

火数云-618限时活动,国内云服务器大连3折,限量50台,九江7折 限量30台!

官方网站:点击访问火数云活动官网活动方案:CPU内存硬盘带宽流量架构IP机房价格购买地址4核4G50G 高效云盘20Mbps独享不限openstack1个九江287元/月立即抢购4核8G50G 高效云盘20Mbps独享不限openstack1个九江329元/月立即抢购2核2G50G 高效云盘5Mbps独享不限openstack1个大连15.9元/月立即抢购2核4G50G 高效云盘5Mbps独享不限...

android开发工具为你推荐
依赖注入什么是侵入性?还有依赖注入?百度手写百度如何手写:天天酷跑刷金币天天酷跑怎么刷金币?ios系统苹果手机的系统是什么?iphone6上市时间苹果6什么时候出?什么是云平台什么是云系统?网站优化方案网站优化方法有哪些三星s8什么时候上市三星盖乐世S8上市时间公布 三星盖乐世s8多少钱blogcn远目是什么意思?厦门铁通厦门铁通宽带可以免费升级到2m么?
台湾虚拟主机 域名主机空间 主机优惠码 winscp 美元争夺战 便宜建站 宕机监控 回程路由 论坛空间 java虚拟主机 777te cpanel空间 howfile 最好的免费空间 hkg nerds 阿里校园 服务器是干什么的 qq对话框 网通服务器托管 更多