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

月神科技 国内上新成都高防 全场八折促销续费同价!

月神科技是由江西月神科技有限公司运营的一家自营云产品的IDC服务商,提供香港安畅、香港沙田、美国CERA、成都电信等机房资源,月神科技有自己的用户群和拥有创宇认证,并且也有电商企业将业务架设在月神科技的平台上。本次带来的是全场八折促销,续费同价。并且上新了国内成都高防服务器,单机100G集群1.2T真实防御,上层屏蔽UDP,可定制CC策略。非常适合网站用户。官方网站:https://www.ysi...

百纵科技云主机首月9元,站群1-8C同价,美国E52670*1,32G内存 50M 899元一月

百纵科技:美国高防服务器,洛杉矶C3机房 独家接入zenlayer清洗 带金盾硬防,CPU全系列E52670、E52680v3 DDR4内存 三星固态盘阵列!带宽接入了cn2/bgp线路,速度快,无需备案,非常适合国内外用户群体的外贸、搭建网站等用途。官方网站:https://www.baizon.cnC3机房,双程CN2线路,默认200G高防,3+1(高防IP),不限流量,季付送带宽美国洛杉矶C...

pacificrack:2021年七夕VPS特别促销,$13.14/年,2G内存/2核/60gSSD/1T流量,支持Windows

pacificrack官方在搞2021年七夕促销,两款便宜vps给的配置都是挺不错的,依旧是接入1Gbps带宽,KVM虚拟、纯SSD raid10阵列,支持包括Linux、Windows 7、10、server2003、2008、2012、2016、2019在内多种操作系统。本次促销的VPS请特别注意限制条件,见本文末尾!官方网站:https://pacificrack.com支持PayPal、支...

android开发工具为你推荐
主页改不了为什么我电脑的主页改不了了?bluestacks安卓模拟器BlueStacks如何安装使用?渗透测试软件测试与渗透测试那个工作有前途1433端口怎么去看1433端口网站运营网络运营具体做什么呢在线代理网站求有效的代理服务器地址?直播加速怎么让已拍摄好的视频加速迅雷云点播账号求个迅雷VIP 是VIP就可以 只用来看云点播 改密码是孙子。 谢了 ! 362135668@qq.com二层交换机什么是二层交换机和三层交换机???iphone6上市时间苹果6什么时候出来
上海服务器租用 国外免费vps 骨干网 cybermonday 鲨鱼机 腾讯云数据库 免费cdn加速 小米数据库 京东商城0元抢购 双拼域名 hostloc 100m独享 linux服务器维护 网通服务器托管 中国电信宽带测速网 789电视剧 smtp虚拟服务器 阿里云免费邮箱 谷歌台湾 免费个人主页 更多