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

CheapWindowsVPS$4.5/月,美国VPS/免费Windows系统/1Gbps不限流量/,可选美洲、欧洲、亚洲等8大机房

国外商家提供Windows系统的并不常见,CheapWindowsVPS 此次提供的 2 款 VPS 促销套餐,提供 5 折永久优惠码,优惠后月付 4.5 美元起,价格还是挺诱人的,VPS 不限流量,接入 1Gbps 带宽,8 个机房皆可选,其中洛杉矶机房还提供亚洲优化网络供选择,操作系统有 Windows 10 专业版、2012 R2、2016、Linux等。Cheap Windows VPS是...

享有云:美国BGP云服务器低至20元/月起,首月打折;香港2核2G2M仅50元/月起

享有云怎么样?享有云是一家新的国内云服务器商家,目前提供国内、香港及海外地区的云服务器,拥有多线路如:BGP线路、CN2线路、高防等云服务器,并且提供稳定、安全、弹性、高性能的云端计算服务,实时满足您的多样性业务需求。目前,美国bgp云服务器,5M带宽,低至20元/月起,270元/年起,首月打折;香港2核2G2M仅50元/月起,450元/年起!点击进入:享有云官方网站地址享有云优惠活动:一、美国B...

搬瓦工VPS:高端线路,助力企业运营,10Gbps美国 cn2 gia,1Gbps香港cn2 gia,10Gbps日本软银

搬瓦工vps(bandwagonhost)现在面向中国大陆有3条顶级线路:美国 cn2 gia,香港 cn2 gia,日本软银(softbank)。详细带宽是:美国cn2 gia、日本软银,都是2.5Gbps~10Gbps带宽,香港 cn2 gia为1Gbps带宽,搬瓦工是目前为止,全球所有提供这三种带宽的VPS(云服务器)商家里面带宽最大的,成本最高的,没有第二家了! 官方网站:https...

android开发工具为你推荐
无线路由器限速设置wifi怎么设置限速开机滚动条电脑开机有滚动条的画面xv播放器下载xv播放器怎么下载?qq空间打扮如何打扮QQ空间?宕机宕机是什么意思iphone6上市时间苹果六什么时候出的bluestack安卓模拟器bluestacks怎么用?电子商务网站模板电子商务网站模板哪个好?电子商务网站模板免费建站怎么样?虚拟机软件下载求一个免费虚拟机软件!!!请发送下载网站给我QzongQZONG怎么玩?
合租服务器 域名备案批量查询 淘宝抢红包攻略 唯品秀 cpanel主机 koss 域名优惠码 512m xen 空间服务商 dd444 e蜗 hostloc 河南移动网 1元域名 台湾google 路由跟踪 申请免费空间 web服务器有哪些 日本小学生 更多