栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Windows

通过HttpClient请求Web Service的操作方法

Windows 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过HttpClient请求Web Service的操作方法

  Web Service是一个基于可编程的Web应用程序,由于服务端是用Web Service开发的,如果android要调用Web Service服务来获取数据,就需要通过HttpClient发送post请求来获取,操作方法如下。

  方法/步骤:

  注:本文使用的是查询手机号码归属地的Web。

  1、service:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl。

  查询的主要方法:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo

  2、可以看出该方法的两个传入参数的名称;

  3、下面直接上代码。

  1. 01[java] view plaincopy
  2. 02private void getMobileCodeInfo(){
  3. 03try {
  4. 04final String SERVER_URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; // 定义需要获取的内容来源地址
  5. 05HttpPost request = new HttpPost(SERVER_URL);
  6. 06List params = new ArrayList();
  7. 07params.add(new BasicNamevaluePair("mobileCode", "136370628")); //(注意这里的号码必须大于6位)
  8. 08params.add(new BasicNamevaluePair("userId", ""));
  9. 09request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  10. 10HttpResponse httpResponse = new DefaultHttpClient().execute(request);
  11. 11if (httpResponse.getStatusLine().getStatusCode() != 404)
  12. 12{
  13. 13String result = EntityUtils.toString(httpResponse.getEntity());
  14. 14System.out.println(result);
  15. 15}
  16. 16} catch (Exception e) {
  17. 17Log.e("eee", ""+e);
  18. 18e.printStackTrace();
  19. 19}
  20. 20}
复制代码 [java] view plaincopy private void getMobileCodeInfo(){ try { final String SERVER_URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; // 定义需要获取的内容来源地址 HttpPost request = new HttpPost(SERVER_URL); List params = new ArrayList(); params.add(new BasicNamevaluePair("mobileCode", "136370628")); //(注意这里的号码必须大于6位) params.add(new BasicNamevaluePair("userId", "")); request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = new DefaultHttpClient().execute(request); if (httpResponse.getStatusLine().getStatusCode() != 404) { String result = EntityUtils.toString(httpResponse.getEntity()); System.out.println(result); } } catch (Exception e) { Log.e("eee", ""+e); e.printStackTrace(); } }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/563470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号