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

A2Hosting三年付$1.99/月,庆祝18周年/WordPress共享主机最高优惠81%/100GB SSD空间/无限流量

A2Hosting主机,A2Hosting怎么样?A2Hosting是UK2集团下属公司,成立于2003年的老牌国外主机商,产品包括虚拟主机、VPS和独立服务器等,数据中心提供包括美国、新加坡softlayer和荷兰三个地区机房。A2Hosting在国外是一家非常大非常有名气的终合型主机商,拥有几百万的客户,非常值得信赖,国外主机论坛对它家的虚拟主机评价非常不错,当前,A2Hosting主机庆祝1...

Sparkedhost($8/月)美国迈阿密AMD Ryzen高性能VPS;免费100G高防

sparkedhost怎么样?sparkedhost主机。Sparkedhost于2017年7月注册在美国康涅狄格州,2018年收购了ClynexHost,2019年8月从Taltum Solutions SL收购了The Beast Hosting,同年10月从Reilly Bauer收购了OptNode Hosting。sparkedhost当前的业务主要为:为游戏“我的世界”提供服务器、虚拟...

VoLLcloud7折月付$3,香港CMI云服务器原生IP解锁,香港VoLLcloud

vollcloud怎么样?vollcloud LLC创立于2020年,是一家以互联网基础业务服务为主的 技术型企业,运营全球数据中心业务。VoLLcloud LLC针对新老用户推出全场年付产品7折促销优惠,共30个,机会难得,所有产品支持3日内无条件退款,同时提供产品免费体验。目前所有产品中,“镇店之宝”产品性价比高,适用大部分用户基础应用,卖的也是最好,同时,在这里感谢新老用户的支持和信任,我们...

android开发工具为你推荐
ps移花接木教程教教我PS的移花接木万网核心代理哪里可以注册免费代理?网易公开课怎么下载如何下载网易公开课硬盘人移动硬盘的优缺点lockdowndiphone4s 完美越狱5.1.1时出现Could not connect to lockdownd。求救啊!!网络广告投放怎样在网络上进行广告的投放?小米手柄买了个小米蓝牙手柄,游戏是可以玩但是按键位置不舒服,怎么可以改按键虚拟机软件下载谁有好用的虚拟机软件?网络虚拟机VMware虚拟机三种网络模式的区别有哪些?声母是什么声母.韵母有哪些
godaddy域名解析教程 greengeeks payoneer Hello图床 2017年黑色星期五 最好看的qq空间 绍兴高防 java虚拟主机 灵动鬼影 老左来了 支付宝扫码领红包 免费网页申请 外贸空间 服务器维护 cxz 云服务器比较 申请免费空间 rewritecond 美国主机侦探 sonya 更多